﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using SmallERP.MODEL.PBModel;
using System.Drawing;
using System.Diagnostics;
using System.Data.Common;
using System.Data;
using SmallERP.Bll.Com;
//using SmallERP.Bll.MakeBigPic;
using SmallERP.COM;
using Smallerp.Contracts;
using Smallerp.Contracts.DataContracts;
using System.Collections;
using LayoutDll;


namespace SmallERP.Bll.PinBan
{
    /// <summary>
    /// 处理拼板模块中关于定单表的逻辑关系
    /// </summary>
    public class OrderBLL
    {
        public const int PERINCHTOSCREEN = 10;//1英寸=10屏（相素点）
        public bool IsNewWhirl = false;//自动拼板是否又进行循环拼板
        public const int BARCODEWIDTH = 30;//条码点25个象素点宽

        private string _addr;
        private int _port;
        private string _dogCode;

        public bool IsAutoStop = false;

        LayoutDll.ClassInterFace InterFace = new LayoutDll.ClassInterFace();
        int[] BlackWidth = new Int32[3] { 200, 300, 400 };
        int[] ComboxNum = new Int32[3] { 2, 3, 4 };
        int[] CountLevel = new Int32[3] { 0, 0, 0 };

        int[] BlackWidthLong = new Int32[3] { 10, 15, 20 };
        int[] ComboxNumLong = new Int32[3] { 2, 3, 4 };
        int[] CountLevelLong = new Int32[3] { 0, 0, 0 };
        int changeLevel = 0;
        int tryTime = 0;

        int[] BlackWidthForce = new Int32[3] { 10, 15, 20 };
        int[] ComboxNumForce = new Int32[3] { 2, 3, 4 };
        int[] CountLevelForce = new Int32[3] { 3, 4, 5 };
        int[] BlackWidthLowWaste = new Int32[3] { 3, 8, 15 };
        int[] ComboxNumLowWaste = new Int32[3] { 2, 3, 4 };
        int[] CountLevelLowWaste = new Int32[3] { 3, 4, 5 };
        private string GetDogCode()
        {
            if (string.IsNullOrEmpty(_dogCode))
            {
                // _dogCode = UnityMethod.GetDogCode(_addr, _port);
                // _dogCode = UnityMethod.GetDogCode("192.168.1.13", 8888);
            }

            return _dogCode;
        }
        IOperateDB _OperateDb;
        public OrderBLL(string addr, int port, IOperateDB inOper)
        {
            _addr = addr;
            _port = port;
            _OperateDb = inOper;
            //try
            //{
              BoardFolderPath = (String)(_OperateDb.ExecuteScalar("select BigPicSavePath from SysSetting"));
            //}catch(Exception ex)
            //{


            //}
        }
        string BoardFolderPath = "";
        public int GetAllOrderInfoByTemplate(BoardTemplate_DCT tInfo, PrinterDCT printerInfo, out List<OrderInfo_PBDCT> resltOrders)
        {
            resltOrders = new List<OrderInfo_PBDCT>();
            int photoStatus_PB = (int)SmallERP.COM.Status.PhotoStatus.WaitPB;//等待拼板
            int orderStatus_PB = (int)SmallERP.COM.Status.OrderStatus.Doing;//拼板中
            if (tInfo == null)
            {
                return -1;
            }
            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    foreach (var oit in db.orderInfo.Where(o => o.PaperFaceId == tInfo.PaperFaceId && o.PaperTypeId == tInfo.PaperTypeId && o.Status == orderStatus_PB).ToList())
            //    {
            //        OrderInfo_PBDCT oi_INfoT = new OrderInfo_PBDCT();
            //        oi_INfoT.order_Base_Info = new Order_Info()
            //        {
            //            CompleteTime = oit.CompleteTime,
            //            OrderName = oit.OrderName,
            //            OrderPath = oit.OrderPath,
            //            OrderProductId = oit.OrderProductId,
            //            Priority = oit.Priority,
            //            ProductNum = oit.ProductNum,
            //            Remark = oit.Remark,
            //            SameFlagOrderId = oit.SameFlagOrderId,
            //            StartTime = oit.StartTime,
            //            Status = oit.Status,
            //            PaperFaceId = oit.PaperFaceId,
            //            PaperTypeId = oit.PaperTypeId,
            //            OrilOrderPath = oit.OrilOrderPath
            //        };
            //        if (!oit.photoInfo.IsLoaded)
            //        {
            //            oit.photoInfo.Load();
            //        }

            //        foreach (var pit in oit.photoInfo.Where(p => p.Status == photoStatus_PB).ToList())
            //        {
            //            oi_INfoT.PhotoList.Add(new PhotoInfo_PBDCT()
            //            {
            //                BarCodeStatus = tInfo.BarCodeStatus,
            //                IsAutoPB = false,
            //                IsInBoard = false,
            //                IsWhirl = false,
            //                Location = new Point(0, 0),
            //                OrderName = oit.OrderName,
            //                PaperFaceName = tInfo.PaperFaceName,
            //                PaperTypeName = tInfo.PaperTypeName,
            //                Priority = oit.Priority,
            //                PBNum = 0,
            //                Photo_Base_Info = new Photo_Info()
            //                {
            //                    Height = pit.Height,
            //                    IsReback = pit.IsReback,
            //                    OrderProductId = oit.OrderProductId,
            //                    PaperFaceId = tInfo.PaperFaceId,
            //                    PaperTypeID = tInfo.PaperTypeId,
            //                    PhotoId = pit.PhotoId,
            //                    PhotoName = pit.PhotoName,
            //                    PicPath = pit.PicPath,
            //                    PixHeight = Convert.ToInt32(pit.Height * printerInfo.PDPI),
            //                    PixWidth = Convert.ToInt32(pit.Width * printerInfo.PDPI),
            //                    Remark = pit.Remark,
            //                    SameFlagPhotoId = pit.SameFlagPhotoId,
            //                    Status = pit.Status,
            //                    TotalNum = 1,//份数
            //                    Width = pit.Width,
            //                    UserId = 0,
            //                    PrinterId = 0,
            //                    IsClearBox = pit.IsClearBox,
            //                    PicOripath = pit.PicOripath
            //                }
            //            });
            //            if (pit.IsClearBox)
            //            {
            //                oi_INfoT.IsClearBox = true;
            //            }
            //            if (pit.IsReback)
            //            {
            //                oi_INfoT.HasRebackOrder = true;
            //            }
            //        }
            //        if (oi_INfoT.PhotoList.Count > 0)
            //        {
            //            resltOrders.Add(oi_INfoT);
            //        }

            //    }
            //}
            // string QueryOrderForPB = "select * from orderinfo where PaperFaceId=" + tInfo.PaperFaceId + " and PaperTypeId=" + tInfo.PaperTypeId + " and Status=" + orderStatus_PB;

            string QueryOrderForPB = "select * from orderinfo where status=" + orderStatus_PB + " and orderproductid in (select  distinct orderproductid from photoinfo where status=" + photoStatus_PB + ")";
            // string QueryOrderForPB = "select * from orderinfo where  Status=" + orderStatus_PB;

            DataTable TableOrderInfoPB = _OperateDb.QueryDataTable(QueryOrderForPB);

            for (int i = 0; i < TableOrderInfoPB.Rows.Count; i++)
            {
                OrderInfo_PBDCT oi_INfoT = new OrderInfo_PBDCT();
                oi_INfoT.order_Base_Info = new Order_InfoDCT()
                {
                    CompleteTime = Convert.ToDateTime(TableOrderInfoPB.Rows[i]["CompleteTime"]),
                    OrderName = Convert.ToString(TableOrderInfoPB.Rows[i]["OrderName"]),
                    OrderPath = Convert.ToString(TableOrderInfoPB.Rows[i]["OrderPath"]),
                    OrderProductId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["OrderProductId"]),
                    OrilOrderPath = Convert.ToString(TableOrderInfoPB.Rows[i]["OrilOrderPath"]),
                    PaperFaceId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["PaperFaceId"]),
                    PaperTypeId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["PaperTypeId"]),
                    Priority = Convert.ToInt32(TableOrderInfoPB.Rows[i]["Priority"]),
                    ProductNum = Convert.ToInt32(TableOrderInfoPB.Rows[i]["ProductNum"]),
                    Remark = Convert.ToString(TableOrderInfoPB.Rows[i]["Remark"]),
                    SameFlagOrderId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["SameFlagOrderId"]),
                    StartTime = Convert.ToDateTime(TableOrderInfoPB.Rows[i]["StartTime"]),
                    Status = Convert.ToInt32(TableOrderInfoPB.Rows[i]["Status"]),
                    FatherFolder = Convert.ToString(TableOrderInfoPB.Rows[i]["FatherFolder"]),
                    GranddadFolder = Convert.ToString(TableOrderInfoPB.Rows[i]["GranddadFolder"])
              
//DXFFilePath = Convert.ToString(TableOrderInfoPB.Rows[i]["DXFFilePath"])

                };
            //    if(TableOrderInfoPB.Rows[i]["DXFContent"] is DBNull)
            //    {
            //        oi_INfoT.order_Base_Info.DXFContent = null;
            //    }
            //else
            //    {
            //        oi_INfoT.order_Base_Info.DXFContent = (byte[]) TableOrderInfoPB.Rows[i]["DXFContent"];
            //    }
           
                string QuertPhotoForPB = "select * from Photoinfo where orderproductid=" + Convert.ToInt32(TableOrderInfoPB.Rows[i]["OrderProductId"]) + " and status=" + photoStatus_PB + " and ColorType=" + printerInfo.PrinterType + " and PaperFaceId=" + tInfo.PaperFaceId + " and PaperTypeId=" + tInfo.PaperTypeId;
                DataTable tableQueryPhoto = _OperateDb.QueryDataTable(QuertPhotoForPB);
                oi_INfoT.PhotoList = new List<PhotoInfo_PBDCT>();
                string QuertPhotoCountForPB = "select Count(*) from Photoinfo where orderproductid=" + Convert.ToInt32(TableOrderInfoPB.Rows[i]["OrderProductId"]) + " and ColorType=" + printerInfo.PrinterType;
                int AllPhotoCount = (int)_OperateDb.ExecuteScalar(QuertPhotoCountForPB);
                int unInboardCount = tableQueryPhoto.Rows.Count;
                for (int j = 0; j < unInboardCount; j++)
                {
                    oi_INfoT.PhotoList.Add(new PhotoInfo_PBDCT()
                    {
                        BarCodeStatus = tInfo.BarCodeStatus,
                        IsAutoPB = false,
                        IsInBoard = false,
                        IsWhirl = false,
                        Location = new Point(0, 0),
                        OrderName = Convert.ToString(TableOrderInfoPB.Rows[i]["OrderName"]),
                        PaperFaceName = tInfo.PaperFaceName,
                        PaperTypeName = tInfo.PaperTypeName,
                        PBNum = 0,
                        Priority = Convert.ToInt32(TableOrderInfoPB.Rows[i]["Priority"]),
                        OrderPhotosCount = AllPhotoCount,
                        OrderPhotosInboardCount = AllPhotoCount - unInboardCount,
                        Photo_Base_Info = new PhotoDCT()
                        {
                            Height = Convert.ToDouble(tableQueryPhoto.Rows[j]["Height"]),
                            IsClearBox = Convert.ToBoolean(tableQueryPhoto.Rows[j]["IsClearBox"]),
                            IsReback = Convert.ToBoolean(tableQueryPhoto.Rows[j]["IsReback"]),
                            OrderProductId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["OrderProductId"]),
                            PaperFaceId = Convert.ToInt32(TableOrderInfoPB.Rows[i]["PaperFaceId"]),
                            PaperTypeID = Convert.ToInt32(TableOrderInfoPB.Rows[i]["PaperTypeId"]),
                            PhotoId = Convert.ToInt32(tableQueryPhoto.Rows[j]["PhotoId"]),
                            PhotoName = Convert.ToString(tableQueryPhoto.Rows[j]["PhotoName"]),
                            PicOripath = Convert.ToString(tableQueryPhoto.Rows[j]["PicOripath"]),
                            PicPath = Convert.ToString(tableQueryPhoto.Rows[j]["PicPath"]),
                            PixHeight = Convert.ToInt32(tableQueryPhoto.Rows[j]["PixHeight"]),
                            PixWidth = Convert.ToInt32(tableQueryPhoto.Rows[j]["PixWidth"]),
                            PrinterId = Convert.ToInt32(tableQueryPhoto.Rows[j]["PrinterId"]),
                            Remark = Convert.ToString(tableQueryPhoto.Rows[j]["Remark"]),
                            SameFlagPhotoId = Convert.ToInt32(tableQueryPhoto.Rows[j]["SameFlagPhotoId"]),
                            Status = Convert.ToInt32(tableQueryPhoto.Rows[j]["Status"]),
                            TotalNum = Convert.ToInt32(tableQueryPhoto.Rows[j]["TotalNum"]),
                            UserId = Convert.ToInt32(tableQueryPhoto.Rows[j]["UserId"]),
                            Width = Convert.ToDouble(tableQueryPhoto.Rows[j]["Width"]),
                            DPI = Convert.ToInt32(tableQueryPhoto.Rows[j]["DPI"]),
                            ColorType = Convert.ToInt32(tableQueryPhoto.Rows[j]["ColorType"]) ,
                            DXFFilePath = Convert.ToString(tableQueryPhoto.Rows[j]["DXFFilePath"]),
                            DXFContent = tableQueryPhoto.Rows[j]["DXFContent"] is DBNull ? null : (byte[])(tableQueryPhoto.Rows[j]["DXFContent"])
                        }
                                    

                    });
                    if (Convert.ToBoolean(tableQueryPhoto.Rows[j]["IsClearBox"]))
                    {
                        oi_INfoT.IsClearBox = true;
                    }
                    if (Convert.ToBoolean(tableQueryPhoto.Rows[j]["IsReback"]))
                    {
                        oi_INfoT.HasRebackOrder = true;
                    }

                }
                //if (oi_INfoT.PhotoList.Count>0)
                if (oi_INfoT.PhotoList.Count > 0)
                {
                    resltOrders.Add(oi_INfoT);
                }
            }

            return 1;
        }
        public List<BoardInfoDCT> GetBoardInfo(string BoardName, DateTime inSTTime, DateTime inEndTime)
        {
            //return _OperateDb.GetBoardInfo(BoardName, inSTTime, inEndTime);

            List<BoardInfoDCT> listRtn = new List<BoardInfoDCT>();



            string BoardListUnSureSql = "";
            if (!string.IsNullOrEmpty(BoardName))
            {
                BoardListUnSureSql = "select * from board where picPath like'%" + BoardName + "%' and createtime <#" + inEndTime + "# and createtime>#" + inSTTime + "#";
            }
            else
            {
                BoardListUnSureSql = "select * from board where  createtime <#" + inEndTime + "# and createtime>#" + inSTTime + "#";

            }

            DataTable BoardTableUnSure = _OperateDb.QueryDataTable(BoardListUnSureSql);
            for (int i = 0; i < BoardTableUnSure.Rows.Count; i++)
            {
                BoardInfoDCT board = new BoardInfoDCT();

                board.Width = (decimal)(Convert.ToDouble(BoardTableUnSure.Rows[i]["Length"]) / Convert.ToInt32(BoardTableUnSure.Rows[i]["DPI"]) * 2.54);
                board.Height = (decimal)(Convert.ToDouble(BoardTableUnSure.Rows[i]["Height"]) / Convert.ToInt32(BoardTableUnSure.Rows[i]["DPI"]) * 2.54);
                board.UsefulNum = Math.Round((1 - Convert.ToDecimal(BoardTableUnSure.Rows[i]["WasteRate"])) * 100, 2);
                board.BoardPath = (string)BoardTableUnSure.Rows[i]["picPath"];
                board.BoardName = board.BoardPath.Substring(0, board.BoardPath.LastIndexOf("."));

                string QcountPhoto = "select SUM(Height*Width*2.54*2.54/10000) AS photoSum from PicMakeUp,photoinfo where boardid=" + Convert.ToInt32(BoardTableUnSure.Rows[i]["BoardId"]) + " and photoinfo.PhotoId=PicMakeUp.PhotoId";
                var rtn = _OperateDb.QueryDataTable(QcountPhoto);
                if (rtn.Rows.Count == 0)
                {
                    board.PhotosAreas = 0;
                }
                else
                {
                    if (rtn.Rows[0][0] is DBNull)
                    {
                        board.PhotosAreas = 0;
                    }
                    else
                    {
                        board.PhotosAreas = Math.Round(Convert.ToDecimal(rtn.Rows[0][0]), 4); ;
                    }
                }


                //listRtnboard.UseTime = 0;
                listRtn.Add(board);


            }
            return listRtn;
        }
        public List<PhotoSeach_DCT> GetSeachPhoto(string name, DateTime stTime, DateTime endTime)
        {
            List<PhotoSeach_DCT> rtnList = new List<PhotoSeach_DCT>();
            string seachPhotoSql = "";

            string SeleteClearType = "select SysOrderPath  from SysSetting";
            string toPath = "";
            string pathT = (string)_OperateDb.QueryDataTable(SeleteClearType).Rows[0][0];
            if ("keep".Equals(pathT))
            {
                //不改变
            }
            else if ("delete".Equals(pathT))
            {

            }
            else
            {
                toPath = pathT;
            }
            if (string.IsNullOrEmpty(name))
            {
                seachPhotoSql = "select PhotoName,OrderName ,photoinfo.Height,photoinfo.Width,photoinfo.PicPath as PPath,board.picPath as Bpath,OrderPath,photoOrderNum,Board.boardid as BoardID from photoinfo,orderinfo,board,picmakeup where photoinfo.orderproductid=orderinfo.orderproductid and photoinfo.photoid=picmakeup.photoid and board.BoardId=picmakeup.BoardId  and board.CreateTime<#" + endTime + "# and board.CreateTime>#" + stTime + "#";
            }
            else
            {
                //seachPhotoSql = "select PhotoName,orderproductname,Height,Width,photoinfo.PicPath PPath,board.picPath  Bpath from photoinfo,orderinfo,board,picmakeup where photoinfo.orderpaoductid=orderinfo.orderproductid and photoinfo.photoid=picmakeup.photoid and baord.boardid=picmakeup.boardid and photoinfo.picpath like '%" + name + "%' and board.CreateTime<#" + endTime + "# and board.CreateTime>#" + stTime + "#";
                seachPhotoSql = "select PhotoName,OrderName ,photoinfo.Height,photoinfo.Width,photoinfo.PicPath as PPath,board.picPath as Bpath,OrderPath,photoOrderNum,Board.boardid as BoardID  from photoinfo,orderinfo,board,picmakeup where photoinfo.orderproductid=orderinfo.orderproductid and photoinfo.photoid=picmakeup.photoid and board.BoardId=picmakeup.BoardId  and  photoinfo.picpath like '%" + name + "%' and board.CreateTime<#" + endTime + "# and board.CreateTime>#" + stTime + "#";

            }
            DataTable TableINfo = _OperateDb.QueryDataTable(seachPhotoSql);
            for (int j = 0; j < TableINfo.Rows.Count; j++)
            {
                PhotoSeach_DCT psd = new PhotoSeach_DCT()
                {
                    Height = Convert.ToDouble(TableINfo.Rows[j]["Height"]),
                    PicOripath = Convert.ToString(TableINfo.Rows[j]["Ppath"]),
                    PicPath = Convert.ToString(TableINfo.Rows[j]["PPath"]),
                    Width = Convert.ToDouble(TableINfo.Rows[j]["Width"]),
                    OrderProductName = Convert.ToString(TableINfo.Rows[j]["OrderName"]),
                    BoardPath = Convert.ToString(TableINfo.Rows[j]["Bpath"]),
                    PhotoName = Convert.ToString(TableINfo.Rows[j]["PhotoName"]),
                    ToPath = (Convert.ToString(TableINfo.Rows[j]["PPath"])).Replace(Convert.ToString(TableINfo.Rows[j]["OrderPath"]), toPath + "\\" + Convert.ToString(TableINfo.Rows[j]["OrderName"])),
                    OrderNumInProduct = Convert.ToInt32(TableINfo.Rows[j]["photoOrderNum"] is DBNull ? 0 : TableINfo.Rows[j]["photoOrderNum"]),
                    BoardID = Convert.ToInt32(TableINfo.Rows[j]["BoardID"])

                };
                rtnList.Add(psd);
            }
            return rtnList;
        }
        void CheckLine(ref List<Line> inList, Point startPoint, Point endPoint)
        {
            Line s = inList.FirstOrDefault(p => p.StartPoint == endPoint);//
            if (s != null)
            {
                s.StartPoint = new Point(startPoint.X, startPoint.Y);
            }

            Line e = inList.FirstOrDefault(p => p.EndPoint == startPoint);
            if (e != null)
            {
                e.EndPoint = new Point(endPoint.X, endPoint.Y);
            }

        }
        void AddLine(ref List<Line> inList, Line inAddLine)
        {
            if (inAddLine.StartPoint == inAddLine.EndPoint)
            {
                return;
            }

            Point endPoint = inAddLine.EndPoint;
            Point startPoint = inAddLine.StartPoint;
            if (inList.FirstOrDefault(p => p.StartPoint == startPoint && p.EndPoint == startPoint) != null)
            {
                return;
            }

            Line lineEx = inList.FirstOrDefault(p => (p.StartPoint.X == endPoint.X && p.StartPoint.Y == endPoint.Y) || (p.EndPoint.X == startPoint.X && p.EndPoint.Y == startPoint.Y));//判断是否有现有在一条先上的两条线

            if (lineEx != null)//合并两个在一条直线上的先
            {

                if (lineEx.StartPoint == inAddLine.EndPoint)
                {
                    lineEx.StartPoint = inAddLine.StartPoint;
                }
                if (lineEx.EndPoint == inAddLine.StartPoint)
                {
                    lineEx.EndPoint = inAddLine.EndPoint;
                }

            }
            else
            {
                //bool topIsnull = false;
                //bool bottomIsnull = false;
                //int CombWidth = 100;
                //inList = inList.OrderBy(or => or.StartPoint.X).ToList();
                //Line lineExTopLeft = inList.FirstOrDefault(p => p.StartPoint.X < endPoint.X && p.EndPoint.Y == startPoint.Y);//判断是否有现有在一条先上的两条线)
                //Line lineExTopRight = inList.FirstOrDefault(p => p.StartPoint.X > endPoint.X && p.EndPoint.Y == startPoint.Y);

                //Line lineExBottomLeft = inList.FirstOrDefault(p => p.StartPoint.X < endPoint.X && p.StartPoint.Y == endPoint.Y);//判断是否有现有在一条先上的两条线)
                //Line lineExBottomRight = inList.FirstOrDefault(p => p.StartPoint.X > endPoint.X && p.StartPoint.Y == endPoint.Y);
                //////先比较上面

                //int lineExTopLeftDiff = lineExTopLeft == null ? 0 : startPoint.X - lineExTopLeft.StartPoint.X;
                //int lineExTopRightDiff = lineExTopRight == null ? 0 : lineExTopRight.StartPoint.X - startPoint.X;
                //int lineExBottomLeftDiff = lineExBottomLeft == null ? 0 : endPoint.X - lineExBottomLeft.EndPoint.X;
                //int lineExBottomRightDiff = lineExBottomRight == null ? 0 : lineExBottomRight.StartPoint.X - endPoint.X;
                //if (lineExTopLeftDiff == 0 && lineExTopRightDiff == 0)
                //{
                //    //inList.Add(new Line(startPoint, endPoint));//
                //    topIsnull = true;
                //}
                //else if (lineExTopLeftDiff != 0 && lineExTopRightDiff == 0)
                //{
                //    if (lineExTopLeftDiff <= CombWidth)
                //    {
                //        Point newStartPiont = new Point(startPoint.X, lineExTopLeft.StartPoint.Y);
                //        Point newEndPoint = new Point(endPoint.X, endPoint.Y);
                //        lineExTopLeft.StartPoint = newStartPiont;
                //        lineExTopLeft.EndPoint = newEndPoint;
                //        CheckLine(ref inList, newStartPiont, newEndPoint);
                //    }
                //    else
                //    {
                //        topIsnull = true;
                //    }
                //}
                //else if (lineExTopLeftDiff == 0 && lineExTopRightDiff != 0)
                //{
                //    if (lineExTopRightDiff <= CombWidth)
                //    {
                //        Point newStartPoint = new Point(lineExTopRight.StartPoint.X, lineExTopRight.StartPoint.Y);
                //        Point newEndPoint = new Point(lineExTopRight.StartPoint.X, endPoint.Y);
                //        lineExTopRight.StartPoint = newStartPoint;
                //        lineExTopRight.EndPoint = newEndPoint;
                //        CheckLine(ref inList, newStartPoint, newEndPoint);
                //    }
                //    else
                //    {
                //        topIsnull = true;
                //    }
                //}
                //else if (lineExTopLeftDiff != 0 && lineExTopRightDiff != 0)
                //{
                //    //逻辑不可
                //    topIsnull = true;
                //}

                //if (lineExBottomLeftDiff == 0 && lineExBottomRightDiff == 0)
                //{
                //    bottomIsnull = true;
                //}
                //else if (lineExBottomLeftDiff != 0 && lineExBottomRightDiff == 0)
                //{
                //    if (lineExBottomLeftDiff < CombWidth)
                //    {
                //        //吧要原先的线延长至start的Y位置 往右移动lineExBottomLeftDiff的距离即移动到startpoint的X
                //        Point newStartPoint = startPoint;
                //        Point newEndPoint = new Point(startPoint.X, lineExBottomLeft.EndPoint.Y);
                //        lineExBottomLeft.StartPoint = newStartPoint;
                //        lineExBottomLeft.EndPoint = newEndPoint;
                //        CheckLine(ref inList, newStartPoint, newEndPoint);

                //    }
                //    else
                //    {
                //        bottomIsnull = true;
                //    }

                //}
                //else if (lineExBottomLeftDiff == 0 && lineExBottomRightDiff != 0)
                //{
                //    if (lineExBottomRightDiff < CombWidth)
                //    {
                //        Point newStortPoint = new Point(lineExBottomRight.StartPoint.X, startPoint.Y);
                //        //Point newEndPoint = lineExBottomRight.EndPoint;
                //        lineExBottomRight.StartPoint = newStortPoint;
                //        CheckLine(ref inList, newStortPoint, lineExBottomRight.EndPoint);
                //    }
                //    else
                //    {
                //        bottomIsnull = true;
                //    }
                //}
                //else
                //{
                //    //逻辑不可
                //    bottomIsnull = true;
                //}
                //if (bottomIsnull && topIsnull)
                //{
                //    inList.Add(new Line(startPoint, endPoint));//
                //}

                ////原先
                inList.Add(new Line(startPoint, endPoint));//

            }



        }
        void AddLineBak(ref List<Line> inList, Line inAddLine)
        {
            if (inAddLine.StartPoint == inAddLine.EndPoint)
            {
                return;
            }

            Point endPoint = inAddLine.EndPoint;
            Point startPoint = inAddLine.StartPoint;
            if (inList.FirstOrDefault(p => p.StartPoint == startPoint && p.EndPoint == startPoint) != null)
            {
                return;
            }

            Line linBottom = inList.FirstOrDefault(p => p.StartPoint == endPoint);
            Line lineTop = inList.FirstOrDefault(p => p.EndPoint == startPoint);
            if (lineTop != null && linBottom != null)
            {
                //3线合并
                inList.Add(new Line(lineTop.StartPoint, linBottom.EndPoint));
                inList.Remove(linBottom);
                inList.Remove(lineTop);

            }
            else if (lineTop != null && linBottom == null)
            {
                lineTop.EndPoint = endPoint;
            }
            else if (lineTop == null && linBottom != null)
            {
                linBottom.StartPoint = startPoint;
            }
            else
            {
                inList.Add(new Line(startPoint, endPoint));//
            }

            //Line lineEx = inList.FirstOrDefault(p => p.StartPoint == endPoint || p.EndPoint == startPoint);//判断是否有现有在一条先上的两条线

            //if (lineEx != null)//合并两个在一条直线上的先
            //{

            //    if (lineEx.StartPoint == inAddLine.EndPoint)
            //    {
            //        lineEx.StartPoint = inAddLine.StartPoint;
            //    }
            //    if (lineEx.EndPoint == inAddLine.StartPoint)
            //    {
            //        lineEx.EndPoint = inAddLine.EndPoint;
            //    }

            //}
            //else
            //{
            //    //bool topIsnull = false;
            //    //bool bottomIsnull = false;
            //    //int CombWidth = 100;
            //    //inList = inList.OrderBy(or => or.StartPoint.X).ToList();
            //    //Line lineExTopLeft = inList.FirstOrDefault(p => p.StartPoint.X < endPoint.X && p.EndPoint.Y == startPoint.Y);//判断是否有现有在一条先上的两条线)
            //    //Line lineExTopRight = inList.FirstOrDefault(p => p.StartPoint.X > endPoint.X && p.EndPoint.Y == startPoint.Y);

            //    //Line lineExBottomLeft = inList.FirstOrDefault(p => p.StartPoint.X < endPoint.X && p.StartPoint.Y == endPoint.Y);//判断是否有现有在一条先上的两条线)
            //    //Line lineExBottomRight = inList.FirstOrDefault(p => p.StartPoint.X > endPoint.X && p.StartPoint.Y == endPoint.Y);
            //    //////先比较上面

            //    //int lineExTopLeftDiff = lineExTopLeft == null ? 0 : startPoint.X - lineExTopLeft.StartPoint.X;
            //    //int lineExTopRightDiff = lineExTopRight == null ? 0 : lineExTopRight.StartPoint.X - startPoint.X;
            //    //int lineExBottomLeftDiff = lineExBottomLeft == null ? 0 : endPoint.X - lineExBottomLeft.EndPoint.X;
            //    //int lineExBottomRightDiff = lineExBottomRight == null ? 0 : lineExBottomRight.StartPoint.X - endPoint.X;
            //    //if (lineExTopLeftDiff == 0 && lineExTopRightDiff == 0)
            //    //{
            //    //    //inList.Add(new Line(startPoint, endPoint));//
            //    //    topIsnull = true;
            //    //}
            //    //else if (lineExTopLeftDiff != 0 && lineExTopRightDiff == 0)
            //    //{
            //    //    if (lineExTopLeftDiff <= CombWidth)
            //    //    {
            //    //        Point newStartPiont = new Point(startPoint.X, lineExTopLeft.StartPoint.Y);
            //    //        Point newEndPoint = new Point(endPoint.X, endPoint.Y);
            //    //        lineExTopLeft.StartPoint = newStartPiont;
            //    //        lineExTopLeft.EndPoint = newEndPoint;
            //    //        CheckLine(ref inList, newStartPiont, newEndPoint);
            //    //    }
            //    //    else
            //    //    {
            //    //        topIsnull = true;
            //    //    }
            //    //}
            //    //else if (lineExTopLeftDiff == 0 && lineExTopRightDiff != 0)
            //    //{
            //    //    if (lineExTopRightDiff <= CombWidth)
            //    //    {
            //    //        Point newStartPoint = new Point(lineExTopRight.StartPoint.X, lineExTopRight.StartPoint.Y);
            //    //        Point newEndPoint = new Point(lineExTopRight.StartPoint.X, endPoint.Y);
            //    //        lineExTopRight.StartPoint = newStartPoint;
            //    //        lineExTopRight.EndPoint = newEndPoint;
            //    //        CheckLine(ref inList, newStartPoint, newEndPoint);
            //    //    }
            //    //    else
            //    //    {
            //    //        topIsnull = true;
            //    //    }
            //    //}
            //    //else if (lineExTopLeftDiff != 0 && lineExTopRightDiff != 0)
            //    //{
            //    //    //逻辑不可
            //    //    topIsnull = true;
            //    //}

            //    //if (lineExBottomLeftDiff == 0 && lineExBottomRightDiff == 0)
            //    //{
            //    //    bottomIsnull = true;
            //    //}
            //    //else if (lineExBottomLeftDiff != 0 && lineExBottomRightDiff == 0)
            //    //{
            //    //    if (lineExBottomLeftDiff < CombWidth)
            //    //    {
            //    //        //吧要原先的线延长至start的Y位置 往右移动lineExBottomLeftDiff的距离即移动到startpoint的X
            //    //        Point newStartPoint = startPoint;
            //    //        Point newEndPoint = new Point(startPoint.X, lineExBottomLeft.EndPoint.Y);
            //    //        lineExBottomLeft.StartPoint = newStartPoint;
            //    //        lineExBottomLeft.EndPoint = newEndPoint;
            //    //        CheckLine(ref inList, newStartPoint, newEndPoint);

            //    //    }
            //    //    else
            //    //    {
            //    //        bottomIsnull = true;
            //    //    }

            //    //}
            //    //else if (lineExBottomLeftDiff == 0 && lineExBottomRightDiff != 0)
            //    //{
            //    //    if (lineExBottomRightDiff < CombWidth)
            //    //    {
            //    //        Point newStortPoint = new Point(lineExBottomRight.StartPoint.X, startPoint.Y);
            //    //        //Point newEndPoint = lineExBottomRight.EndPoint;
            //    //        lineExBottomRight.StartPoint = newStortPoint;
            //    //        CheckLine(ref inList, newStortPoint, lineExBottomRight.EndPoint);
            //    //    }
            //    //    else
            //    //    {
            //    //        bottomIsnull = true;
            //    //    }
            //    //}
            //    //else
            //    //{
            //    //    //逻辑不可
            //    //    bottomIsnull = true;
            //    //}
            //    //if (bottomIsnull && topIsnull)
            //    //{
            //    //    inList.Add(new Line(startPoint, endPoint));//
            //    //}

            //    ////原先
            //    inList.Add(new Line(startPoint, endPoint));//

            //}



        }
        /// <summary>
        /// 全自动拼板
        /// </summary>
        /// <param name="sourcePList"></param>
        /// <param name="template"></param>
        /// <param name="CurentPrinter"></param>
        /// <returns></returns>
        //public List<PhotoInfo_PBDCT> AutoPinSN(List<List<PhotoInfo_PBDCT>> list, BoardTemplate_DCT boardTemplate_DCT, PrinterDCT printerDCT, decimal waste)

       
        private bool CheckPhoto(List<PhotoInfo_PBDCT> inList, PhotoInfo_PBDCT inPhoto)
        {
            int iCount = inList.Count;
            PhotoInfo_PBDCT P2 = inPhoto;
            for (int i = 0; i < iCount; i++)
            {

                //i j位置进行判断矩形是否重合
                // return true;
                PhotoInfo_PBDCT P1 = inList[i];


                //两个矩形框的边的一半
                decimal halfP1W = (P1.IsWhirl == true) ? P1.Photo_Base_Info.PixFactHeigth / 2 : P1.Photo_Base_Info.PixFactWidth / 2;
                decimal halfP2W = (P2.IsWhirl == true) ? P2.Photo_Base_Info.PixFactHeigth / 2 : P2.Photo_Base_Info.PixFactWidth / 2;
                decimal halfP1H = (P1.IsWhirl == true) ? P1.Photo_Base_Info.PixFactWidth / 2 : P1.Photo_Base_Info.PixFactHeigth / 2;
                decimal halfP2H = (P2.IsWhirl == true) ? P2.Photo_Base_Info.PixFactWidth / 2 : P2.Photo_Base_Info.PixFactHeigth / 2;

                //两个矩形框的中心位置
                decimal c1x = P1.Location.X + halfP1W;
                decimal c1y = P1.Location.Y + halfP1H;
                decimal c2x = P2.Location.X + halfP2W;
                decimal c2y = P2.Location.Y + halfP2H;
                //横向判断
                if (Math.Abs(c1x - c2x) >= halfP1W + halfP2W || Math.Abs(c1y - c2y) >= halfP1H + halfP2H)
                {
                    return false;
                }
                else
                {
                    return true;
                }



            }
            return false;
        }
        /// <summary>
        /// 全自动拼板
        /// </summary>
        /// <param name="sourcePList"></param>
        /// <param name="template"></param>
        /// <param name="CurentPrinter"></param>
        /// <returns></returns>
        //public List<PhotoInfo_PBDCT> AutoPinSN(List<List<PhotoInfo_PBDCT>> list, BoardTemplate_DCT boardTemplate_DCT, PrinterDCT printerDCT, decimal waste)
        /// <summary>
        /// 全自动的拼版
        /// </summary>
        /// <param name="sourcePList"></param>
        /// <param name="template"></param>
        /// <param name="CurentPrinter"></param>
        /// <param name="waste"></param>
        /// <param name="outWaste"></param>
        /// <param name="outCurrLength"></param>
        /// <returns></returns>
        public List<PhotoInfo_PBDCT> AutoPinSN2(List<PhotoInfo_PBDCT> sourcePList, BoardTemplate_DCT template, PrinterDCT CurentPrinter, decimal waste, out decimal outWaste, out int outCurrLength, int samlltype)
        {
            ///最小宽度
            int boardMinX = Convert.ToInt32(12 * CurentPrinter.PDPI);//打印机最小的出版长度

            int suitMinX = Convert.ToInt32(0.6 * template.Length * CurentPrinter.PDPI);//打印机最小的出版长度
            ///板宽  
            int boardHeight = Convert.ToInt32(template.Height * CurentPrinter.PDPI);
            ///版长
            int boardLenght = Convert.ToInt32(template.Length * CurentPrinter.PDPI);
            int MinX = Convert.ToInt32(template.Length * 0.45 * CurentPrinter.PDPI);//打印机最小的出版长度
            //int boardMinXVal=
            Point PLocation = new Point(0, 0);  //图片的坐标,以实际的大纸的坐标为准  
            outCurrLength = 0;//记录真实照片长度
            // List<PhotoInfo_PBDCT> newList=sourcePList.
            //sourcePList.ForEach(p => p.Photo_Base_Info.PixFactHeigth = (int)(p.Photo_Base_Info.Height * CurentPrinter.PDPI));
            //sourcePList.ForEach(p => p.Photo_Base_Info.PixFactWidth = (int)(p.Photo_Base_Info.Width * CurentPrinter.PDPI));
            foreach (PhotoInfo_PBDCT photo in sourcePList)
            {

                photo.Photo_Base_Info.PixFactHeigth = Convert.ToInt32(Convert.ToDecimal(photo.Photo_Base_Info.PixHeight) / photo.Photo_Base_Info.DPI * CurentPrinter.PDPI);
                photo.Photo_Base_Info.PixFactWidth = Convert.ToInt32(Convert.ToDecimal(photo.Photo_Base_Info.PixWidth) / photo.Photo_Base_Info.DPI * CurentPrinter.PDPI);
            }
            sourcePList = ColnePhotoList(sourcePList);
            PLocation = new Point(0, 0);
            Size source_Size = new Size(boardLenght, boardHeight);
            List<PhotoInfo_PBDCT> resultList = new List<PhotoInfo_PBDCT>();
            List<Line> VerticalLine = new List<Line>();///存放拼过的竖线
            List<MyRectangle> listRect = new List<MyRectangle>();
            //Dictionary<double, List<PhotoInfo_PBDCT>> dictRectList = new Dictionary<double, List<PhotoInfo_PBDCT>>();//存放结果的缓存字典
            //List<WasteAndListPhoto> dictRectList = new List<WasteAndListPhoto>();//存放结果的缓存字典
            Rectangle currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
            int currWidth = 0;//记录当前要补充的块的宽
            int tempHeight = boardHeight;
            int maxWidth = boardLenght;
            int outHeightLeft;
            int outWidthLeft;
            bool CanPut = false;//标志该版可以结束
            double currWate = 0;
            List<Size> HardSize = new List<Size>(); //
            while (true)
            {
                try
                {
                    List<PhotoInfo_PBDCT> TSPlist;
                    if (resultList == null || resultList.Count == 0)//不能作为开头使用
                    {
                        TSPlist = sourcePList.Where(p => p.TryTime == 0 && p.IsInBoard == false && p.Photo_Base_Info.PixFactHeigth <= boardHeight).ToList();
                    }
                    else
                    {
                        TSPlist = sourcePList.Where(p => p.IsInBoard == false && p.Photo_Base_Info.PixFactHeigth <= boardHeight).ToList();

                    }
                    if (TSPlist.Count == 0)
                    {
                        if (CheckWasteAndLength(listRect, suitMinX, (double)waste, boardHeight, out currWate))
                        {
                            break;
                        }
                        else
                        {
                            resultList = new List<PhotoInfo_PBDCT>();
                        }
                        ;//剩下的尝试一遍都无法拼版或者木有照片了

                        break;
                    }

                    List<PhotoInfo_PBDCT> PhotoList = FindSuitPhotos(TSPlist, currRectang, template.BarCodeStatus, out outHeightLeft, samlltype);
                    if (PhotoList == null || PhotoList.Count == 0)
                    {
                        #region//找不到合适的照片来放
                        if (VerticalLine.Count == 1 && CheckWasteAndLength(listRect, suitMinX, (double)waste, boardHeight, out currWate))
                        {

                            break;
                        }
                        if (CanPut)//尾端已经平了检查长度 是否够
                        {

                            //长条形
                            double ow = 0;
                            if (CheckWaste(listRect, (double)waste, boardHeight, out ow))
                            {
                                int max = listRect.Max(p => p.PRactangle.Location.X + p.PRactangle.Width);
                                if (max > MinX)
                                {
                                    outWaste = (decimal)ow;
                                    currWate = ow;
                                    break;
                                }
                                else//长度不够 画框继续
                                {


                                    //将继续找下一个可以填充的块如果找不到情况重试
                                    //获取最小的位置值XY 都是最小的
                                    Line firstLineC = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                                    int minXC = firstLineC.StartPoint.X;
                                    int minYC = firstLineC.StartPoint.Y;
                                    int HeightC = firstLineC.EndPoint.Y - firstLineC.StartPoint.Y;//下个矩形的高等于线的高度
                                    int WidthC = boardLenght - minXC;//下个矩形的长度等于版长减去minx定点的横坐标
                                    currWidth = currRectang.Width;
                                    Rectangle newrangC = new Rectangle(new Point(minXC, minYC), new Size(WidthC, HeightC));

                                    currRectang = newrangC;
                                    //if()

                                    continue;//继续填充
                                }

                            }
                            else
                            {

                                CanPut = false;


                                //下一个轮回
                                sourcePList.ForEach(p => p.IsInBoard = false);
                                resultList = new List<PhotoInfo_PBDCT>();
                                VerticalLine = new List<Line>();
                                listRect = new List<MyRectangle>();
                                currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
                                continue;
                            }

                        }
                        ///建立剩余空白版就是未找到的那块放入List
                        ///空白块分两种一种是横向一种是竖向实际是检查上下两个方向的x取小的
                        ///块的宽需要计算得出而不是当前操作快的宽度
                        //查找空白框的顶点所在的那个照片块
                        MyRectangle priRect = null;
                        int balckRWidth = 0;
                        #region
                        //MyRectangle priRectBottom = listRect.Where(p => p.PRactangle.Location.Y + p.PRactangle.Height == currRectang.Location.Y && p.PRactangle.X + p.PRactangle.Width >= currRectang.Location.X && p.PRactangle.X + p.PRactangle.Width < currRectang.Location.X + currRectang.Width).ToList().OrderByDescending(order => order.PRactangle.X + order.PRactangle.Width).FirstOrDefault();
                        //MyRectangle priRectTop = listRect.Where(p => p.PRactangle.Location.Y == currRectang.Location.Y && p.PRactangle.X + p.PRactangle.Width > currRectang.Location.X && p.PRactangle.X + p.PRactangle.Width < currRectang.Location.X + p.PRactangle.Width).ToList().OrderByDescending(order => order.PRactangle.X + order.PRactangle.Width).FirstOrDefault();
                        //if (priRectBottom != null && priRectTop != null)
                        //{
                        //    if (priRectBottom.PRactangle.Location.X + priRectBottom.PRactangle.Width >= priRectTop.PRactangle.Location.X + priRectTop.PRactangle.Width)
                        //    {
                        //        priRect = priRectBottom;

                        //    }
                        //    else
                        //    {
                        //        priRect = priRectTop;

                        //    }
                        //}
                        //else if (priRectBottom == null && priRectTop != null)
                        //{
                        //    priRect = priRectTop;
                        //    balckRWidth = priRect.PRactangle.X + priRect.PRactangle.Width - currRectang.Location.X;
                        //}
                        //else if (priRectBottom != null && priRectTop == null)
                        //{
                        //    priRect = priRectBottom;
                        //    balckRWidth = priRect.PRactangle.X + priRect.PRactangle.Width - currRectang.Location.X;

                        //}
                        //else
                        //{
                        //    if (VerticalLine.Count == 0)
                        //    {
                        //        break;
                        //    }
                        //    else
                        //    {
                        //////dictRectList.Add(currWate, resultList);
                        //////goto bc;
                        //////将继续找下一个可以填充的块如果找不到情况重试
                        //////获取最小的位置值XY 都是最小的
                        ////Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                        ////int minX = firstLine.StartPoint.X;
                        ////int minY = firstLine.StartPoint.Y;
                        ////int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                        ////int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                        ////currWidth = currRectang.Width;
                        ////Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));

                        ////currRectang = newrang;
                        //////if()
                        //////CanPut = true;
                        ////if (VerticalLine.Count == 1)
                        ////{
                        ////    CanPut = true;
                        ////}
                        ////continue;//继续填充
                        #endregion
                        if (VerticalLine.Count == 1)
                        {
                            CanPut = true;
                            continue;
                        }
                        //重新写的填充小块的方法
                        Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                        if (firstLine == null)
                        {
                            ////下一个轮回
                            //sourcePList.ForEach(p => p.IsInBoard = false);
                            //resultList = new List<PhotoInfo_PBDCT>();
                            //VerticalLine = new List<Line>();
                            //listRect = new List<MyRectangle>();
                            //currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
                            //continue;
                            resultList = new List<PhotoInfo_PBDCT>();
                            break;
                        }

                        Point BlackLocation = firstLine.StartPoint;
                        int BlackH = firstLine.EndPoint.Y - firstLine.StartPoint.Y;
                        int TopLeft = 0, BottomLeft = 0;
                        //查找他的上面的矩形   下边Y值等于Location里的Y, X位于矩形内
                        MyRectangle TopRect = listRect.Where(r => r.PRactangle.Location.Y + r.PRactangle.Height == BlackLocation.Y && BlackLocation.X >= r.PRactangle.Location.X && BlackLocation.X < r.PRactangle.Location.X + r.PRactangle.Width).FirstOrDefault();
                        if (TopRect == null)
                        {
                            TopLeft = -1;//上面没有了
                        }
                        else
                        {
                            TopLeft = TopRect.PRactangle.Location.X + TopRect.PRactangle.Width - BlackLocation.X;
                        }

                        //查找他的下面的矩形   上边Y值等于firstLine终点的Y,   X位于矩形内
                        var a = listRect.Where(r => r.PRactangle.Location.Y == firstLine.EndPoint.Y && firstLine.EndPoint.X >= r.PRactangle.Location.X && firstLine.EndPoint.X < r.PRactangle.Location.X + r.PRactangle.Width);
                        MyRectangle BottomRect = a.FirstOrDefault();
                        if (BottomRect == null)
                        {
                            BottomLeft = -1;//下面没有了
                        }
                        else
                        {
                            BottomLeft = BottomRect.PRactangle.Location.X + BottomRect.PRactangle.Width - firstLine.EndPoint.X;
                        }
                        //计算要用空白补充的最小单位
                        if (TopLeft == -1 && BottomLeft != -1)
                        {
                            balckRWidth = BottomLeft;
                        }
                        else if (TopLeft != -1 && BottomLeft == -1)
                        {
                            balckRWidth = TopLeft;
                        }
                        else if (TopLeft != -1 && BottomLeft != -1)
                        {
                            if (TopLeft >= BottomLeft)
                            {
                                balckRWidth = BottomLeft;
                            }
                            else
                            {
                                balckRWidth = TopLeft;
                            }
                        }
                        else
                        {
                            CanPut = true;
                            continue;
                        }

                        MyRectangle BlackRect = new MyRectangle(new Rectangle(currRectang.Location, new Size(balckRWidth, currRectang.Size.Height)));
                        BlackRect.isUsefull = false;
                        if (BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width > maxWidth)
                        {
                            balckRWidth = maxWidth;
                        }
                        listRect.Add(BlackRect);//找不到合适的照片用白板代替应该控制白板的大小
                        //记录当前的竖线集合以便找到下一个矩形框
                        VerticalLine.Remove(VerticalLine.FirstOrDefault(p => p.StartPoint == BlackRect.PRactangle.Location));

                        Point startPoint = new Point(BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width, BlackRect.PRactangle.Y);
                        Point endPoint = new Point(BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width, BlackRect.PRactangle.Y + BlackRect.PRactangle.Height);


                        AddLine(ref VerticalLine, new Line(startPoint, endPoint));

                        if (VerticalLine.Count == 1)//尾端平齐 检查浪费率
                        {
                            double ow = 0;
                            if (CheckWaste(listRect, (double)waste, boardHeight, out ow))
                            {
                                int max = listRect.Max(p => p.PRactangle.Location.X + p.PRactangle.Width);
                                if (max > MinX)
                                {
                                    outWaste = (decimal)ow;
                                    currWate = ow;
                                    break;
                                }
                                else//长度不够 画框继续
                                {


                                    //将继续找下一个可以填充的块如果找不到情况重试
                                    //获取最小的位置值XY 都是最小的
                                    Line firstLineC = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                                    int minXC = firstLineC.StartPoint.X;
                                    int minYC = firstLineC.StartPoint.Y;
                                    int HeightC = firstLineC.EndPoint.Y - firstLineC.StartPoint.Y;//下个矩形的高等于线的高度
                                    int WidthC = boardLenght - minXC;//下个矩形的长度等于版长减去minx定点的横坐标
                                    currWidth = currRectang.Width;
                                    Rectangle newrangC = new Rectangle(new Point(minXC, minYC), new Size(WidthC, HeightC));

                                    currRectang = newrangC;
                                    //if()

                                    continue;//继续填充
                                }

                            }
                            else
                            {

                                CanPut = false;


                                //下一个轮回
                                sourcePList.ForEach(p => p.IsInBoard = false);
                                resultList = new List<PhotoInfo_PBDCT>();
                                VerticalLine = new List<Line>();
                                listRect = new List<MyRectangle>();
                                currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
                                continue;
                            }
                        }

                        //将继续找下一个可以填充的块如果找不到情况重试
                        //获取最小的位置值XY 都是最小的
                        firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                        int minX = firstLine.StartPoint.X;
                        int minY = firstLine.StartPoint.Y;
                        int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                        int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                        currWidth = currRectang.Width;
                        Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));

                        currRectang = newrang;
                        continue;//继续填充

                        //}
                        #endregion
                    }
                    else
                    {
                        if (listRect.Count == 0)//第一张照片
                        {
                            #region//第一张照片
                            // FristSize=PhotoList.FirstOrDefault().PSize;
                            MyRectangle photoRect = null;
                            CanPut = false;
                            foreach (PhotoInfo_PBDCT photo in PhotoList)
                            {
                                MyRectangle myr = new MyRectangle(photo);
                                myr.isUsefull = true;
                                listRect.Add(myr);
                                photo.IsInBoard = true;
                                resultList.Add(photo);
                                photoRect = myr;
                                //photo.TryTime++;
                                //标记照片已经拼版了包含了副本
                                sourcePList.Where(xp => xp.PhotoId == photo.PhotoId).ToList().ForEach(p => p.IsInBoard = true);
                            }
                            PhotoList.FirstOrDefault().TryTime++;
                            int currRectPhotoWidth = 0;
                            int currRectPhotoHeight = 0;
                            if (PhotoList.FirstOrDefault().BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth;
                                currRectPhotoHeight = (photoRect.Photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH) * PhotoList.Count;
                            }
                            else if ((PhotoList.FirstOrDefault().BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft))
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                                currRectPhotoHeight = photoRect.Photo.Photo_Base_Info.PixFactHeigth * PhotoList.Count;
                            }
                            else
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth;
                                currRectPhotoHeight = photoRect.Photo.Photo_Base_Info.PixFactHeigth * PhotoList.Count;
                            }
                            ////VerticalLine.Remove(VerticalLine.FirstOrDefault(p => p.StartPoint == currRectang.Location));

                            outCurrLength = currRectang.Location.X + currRectPhotoWidth;


                            AddLine(ref VerticalLine, new Line(new Point(currRectPhotoWidth, 0), new Point(currRectPhotoWidth, currRectPhotoHeight)));
                            // VerticalLine.Add();
                            AddLine(ref VerticalLine, new Line(new Point(0, currRectPhotoHeight), new Point(0, boardHeight)));
                            //VerticalLine.Add();
                            //outCurrLength = VerticalLine.Max(p => p.StartPoint.X);

                            Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                            int minX = firstLine.StartPoint.X;
                            int minY = firstLine.StartPoint.Y;
                            int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                            int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                            currWidth = currRectang.Width;
                            Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));
                            currRectang = newrang;
                            continue;//继续填充


                            #endregion
                        }
                        else
                        {
                            #region//将照片放到找到的合适的版上
                            int tHeight = 0;
                            //MyRectangle lastRang = null;
                            ///把照片放入找到的暂存块
                            MyRectangle photoRect = null;
                            foreach (PhotoInfo_PBDCT photo in PhotoList.OrderBy(p => p.Location.Y))
                            {
                                MyRectangle myr = new MyRectangle(photo);
                                myr.isUsefull = true;
                                listRect.Add(myr);//加入暂存块
                                //photo.IsInBoard = true;
                                resultList.Add(photo);//加入结果列表
                                //lastRang = myr;
                                photoRect = myr;
                                //标记照片已经拼版了包含了副本
                                sourcePList.Where(xp => xp.PhotoId == photo.PhotoId).ToList().ForEach(p => p.IsInBoard = true);
                                //tempHeight+=myr.PRactangle.Height;

                            }
                            int currRectPhotoWidth = 0;
                            int currRectPhotoHeight = 0;
                            if (PhotoList.FirstOrDefault().BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth;
                                currRectPhotoHeight = (photoRect.Photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH) * PhotoList.Count;
                            }
                            else if ((PhotoList.FirstOrDefault().BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft))
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                                currRectPhotoHeight = photoRect.Photo.Photo_Base_Info.PixFactHeigth * PhotoList.Count;
                            }
                            else
                            {
                                currRectPhotoWidth = photoRect.Photo.Photo_Base_Info.PixFactWidth;
                                currRectPhotoHeight = photoRect.Photo.Photo_Base_Info.PixFactHeigth * PhotoList.Count;
                            }
                            //实际照片块
                            MyRectangle PhotoRect = new MyRectangle(new Rectangle(currRectang.Location, new Size(currRectPhotoWidth, currRectPhotoHeight)));
                            if (currRectang.Location.X + currRectPhotoWidth > outCurrLength)
                            {
                                outCurrLength = currRectang.Location.X + currRectPhotoWidth;
                            }
                            VerticalLine.Remove(VerticalLine.FirstOrDefault(p => p.StartPoint == currRectang.Location));//移除原先的线                      
                            AddLine(ref VerticalLine, new Line(new Point(PhotoRect.PRactangle.X + currRectPhotoWidth, PhotoRect.PRactangle.Y), new Point(PhotoRect.PRactangle.X + currRectPhotoWidth, PhotoRect.PRactangle.Y + currRectPhotoHeight)));//画右边的线
                            if (PhotoRect.PRactangle.Y + currRectPhotoHeight <= boardHeight)//版宽限制
                            {
                                AddLine(ref VerticalLine, new Line(new Point(PhotoRect.PRactangle.X, PhotoRect.PRactangle.Y + currRectPhotoHeight), new Point(PhotoRect.PRactangle.X, PhotoRect.PRactangle.Y + PhotoRect.PRactangle.Height + outHeightLeft)));//画左边的线
                            }
                            else
                            {

                            }
                            //outCurrLength = VerticalLine.Max(p => p.StartPoint.X);

                            //记录当前的竖线集合以便找到下一个矩形框
                            //获取最小的位置值XY 都是最小的
                            Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                            int minX = firstLine.StartPoint.X;
                            int minY = firstLine.StartPoint.Y;
                            int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                            int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                            currWidth = currRectang.Width;
                            Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));

                            currRectang = newrang;
                            continue;//继续填充

                            #endregion

                        }
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            outWaste = (decimal)currWate;
            List<PhotoInfo_PBDCT> rtnList = new List<PhotoInfo_PBDCT>();
            //resultList = dictRectList.OrderBy(p => p.waste).FirstOrDefault().PhotoList;
            foreach (PhotoInfo_PBDCT photo in resultList.OrderBy(o => o.PhotoId))
            {
                if (photo.IsCopy)
                {
                    photo.IsInBoard = false;
                    PhotoInfo_PBDCT dct = sourcePList.Where(p => p.PhotoId == photo.PhotoId && p.IsCopy == false).FirstOrDefault();
                    dct.Location = photo.Location;
                    dct.IsWhirl = true;
                    dct.IsInBoard = true;
                    dct.BarCodeStatus = photo.BarCodeStatus;
                    //if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                    //{
                    //    dct.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft;
                    //}
                    //else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                    //{
                    //    dct.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom;
                    //}
                    //else
                    //{

                    //}
                    rtnList.Add(dct);
                }
                else
                {
                    rtnList.Add(photo);
                }

            }
            //int maxY = rtnList.Max(p => p.Location.Y + (p.IsWhirl ? p.Photo_Base_Info.PixFactWidth : p.Photo_Base_Info.PixFactHeigth));
            //int maxy2 = rtnList.Max(p => p.Location.Y + (p.IsWhirl ? Convert.ToInt32(((double)p.Photo_Base_Info.PixWidth) / p.Photo_Base_Info.DPI * CurentPrinter.PDPI) : Convert.ToInt32(((double)p.Photo_Base_Info.PixHeight) / p.Photo_Base_Info.DPI * CurentPrinter.PDPI)));
            return rtnList;
        }


     
        void AddInBoardNum(List<PhotoInfo_PBDCT> inlist, int inOrderid)
        {
            inlist.Where(o => o.OrderProductId == inOrderid).ToList().ForEach(p => p.OrderPhotosInboardCount++);
        }
        void SubtractInBoardNum(List<PhotoInfo_PBDCT> inlist, int inOrderid)
        {
            inlist.Where(o => o.OrderProductId == inOrderid).ToList().ForEach(p => p.OrderPhotosInboardCount--);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourcePList">所有要拼版照片列表</param>
        /// <param name="template">选定的模板</param>
        /// <param name="CurentPrinter">选定的打印机</param>
        /// <param name="waste">设定的浪费率</param>
        /// <param name="outWaste">当前返回的浪费率</param>
        /// <param name="outCurrLength">当前版的长度</param>
        /// <param name="minLength">最小版长</param>
        /// <param name="isForcePrint">强制出片0普通模式1强制清片模式</param>
        /// <param name="autoModel">0,剩纸模式1，大片模式</param>
        /// <returns></returns>
        public List<PhotoInfo_PBDCT> AutoPinSN21(List<PhotoInfo_PBDCT> sourcePList, BoardTemplate_DCT template, PrinterDCT CurentPrinter, decimal waste, out decimal outWaste, out int outCurrLength, int minLength, bool isForcePrint, int autoModel)
        {
            //isForcePrint = isForcePrint;
            changeLevel = 0;
            waste = waste / 100;
            tryTime = 0;
            ///最小宽度
            int boardMinX = Convert.ToInt32(12 * CurentPrinter.PDPI);//打印机最小的出版长度

            int suitMinX = Convert.ToInt32(0.7 * template.Length * CurentPrinter.PDPI);//打印机最小的出版长度
            ///板宽  
            int boardHeight = Convert.ToInt32(template.Height * CurentPrinter.PDPI);
            ///版长
            int boardLenght = Convert.ToInt32(template.Length * CurentPrinter.PDPI);
            int MinX = Convert.ToInt32(template.Length * ((float)minLength / 100) * CurentPrinter.PDPI);//打印机最小的出版长度
            //int boardMinXVal=
            Point PLocation = new Point(0, 0);  //图片的坐标,以实际的大纸的坐标为准  
            outCurrLength = 0;//记录真实照片长度

            foreach (PhotoInfo_PBDCT photo in sourcePList)
            {

                photo.Photo_Base_Info.PixFactHeigth = Convert.ToInt32(Convert.ToDecimal(photo.Photo_Base_Info.PixHeight) / photo.Photo_Base_Info.DPI * CurentPrinter.PDPI);
                photo.Photo_Base_Info.PixFactWidth = Convert.ToInt32(Convert.ToDecimal(photo.Photo_Base_Info.PixWidth) / photo.Photo_Base_Info.DPI * CurentPrinter.PDPI);
                photo.IsInBoard = false;
                photo.IsWhirl = false;
                //photo.TryTime = 0;
            }
            //sourcePList = ColnePhotoList(sourcePList);
            PLocation = new Point(0, 0);
            Size source_Size = new Size(boardLenght, boardHeight);
            List<PhotoInfo_PBDCT> resultList = new List<PhotoInfo_PBDCT>();
            List<Line> VerticalLine = new List<Line>();///存放拼过的竖线
            List<Point> CurrVLinePhotoPoint = new List<Point>();
            List<MyRectangle> listRect = new List<MyRectangle>();

            Rectangle currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
            int currWidth = 0;//记录当前要补充的块的宽
            int tempHeight = boardHeight;
            int maxWidth = boardLenght;
            int outHeightLeft;
            int outWidthLeft;

            //用户计算回滚操作
            bool CanPut = false;//标志该版可以结束
            //int CanPutNum = 0;//可以出的前几张
            double CanPutWaste = 1;//当前的浪费率rr

            List<int> CutPhotoidList = new List<int>();//将能够出的照片暂存
            Rectangle CutRectangle = new Rectangle();//达到回滚条件时的最小矩形框
            double currWate = 0;
            //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "数据准备成功");
            while (true)
            {

                //try
                //{

                List<PhotoInfo_PBDCT> TSPlist;
                try
                {
                    TSPlist = sourcePList.Where(p => p.IsInBoard == false && ((p.Photo_Base_Info.PixFactHeigth <= boardHeight && p.Photo_Base_Info.PixFactWidth <= boardLenght) || (p.Photo_Base_Info.PixFactHeigth <= boardLenght && p.Photo_Base_Info.PixFactWidth <= boardHeight))).ToList(); ;
                    if (TSPlist.Count == 0)
                    {
                        CanPutWaste = currWate;
                        CutPhotoidList = new List<int>();
                        foreach (PhotoInfo_PBDCT photoct in resultList)
                        {
                            CutPhotoidList.Add(photoct.PhotoId);
                        }
                        break;//直接出因为没有照片了
                    }
                    if (IsAutoStop)
                    {
                        resultList.ForEach(p => p.IsInBoard = false);

                        resultList = new List<PhotoInfo_PBDCT>();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("初始化 出错");
                }

                #region//处理照片放置顺序
                int putPhotoOrder = -1;//0照片宽度按照小大排 1照片宽度按照大小排-1初始值
                Point leftTop = currRectang.Location;//左上定点
                Point leftBottom = new Point(currRectang.Location.X, currRectang.Location.Y + currRectang.Height);//坐下顶点
                try
                {
                    if ((leftBottom.Y - leftTop.Y) == boardHeight)
                    {
                        putPhotoOrder = 1;
                    }
                    else
                    {
                        if (leftTop.Y == 0)
                        {
                            putPhotoOrder = 1;
                        }
                        if (leftTop.Y != 0 && leftBottom.Y == boardHeight)
                        {
                            putPhotoOrder = 0;
                        }
                        if (putPhotoOrder == -1)
                        {
                            var topRect1 = listRect.Where(p => p.LeftBottomPoint.X < leftTop.X && p.RightBottomPoint.X >= leftTop.X);
                            MyRectangle topRect = listRect.FirstOrDefault(p => p.LeftBottomPoint.X <= leftTop.X && p.RightBottomPoint.X > leftTop.X && p.LeftBottomPoint.Y == leftTop.Y);
                            MyRectangle bottomRect = listRect.FirstOrDefault(p => p.LeftTopPoint.X <= leftBottom.X && p.RighTopPoint.X > leftBottom.X && p.LeftTopPoint.Y == leftBottom.Y);
                            if (topRect == null || bottomRect == null)
                            {
                                putPhotoOrder = 1;
                            }
                            if (topRect != null && bottomRect != null)
                            {
                                int topLength = topRect.RighTopPoint.X - leftTop.X;
                                int bottomLength = bottomRect.RightBottomPoint.X - leftBottom.X;
                                if (topLength >= bottomLength)
                                {
                                    putPhotoOrder = 0;
                                }
                                else
                                {
                                    putPhotoOrder = 1;
                                }
                            }



                        }

                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("计算顺序出错");
                }
                #endregion

                #region//获取要拼版的照片
                //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "确定照片顺序成功");
                List<PhotoInfo_PBDCT> PhotoList = new List<PhotoInfo_PBDCT>();
                try
                {
                    if (!isForcePrint)//普通模式
                    {
                        PhotoList = FindSuitPhotos21(TSPlist, currRectang, template.BarCodeStatus, out outHeightLeft, sourcePList, putPhotoOrder, template, CurentPrinter, MinX, autoModel, CurrVLinePhotoPoint);
                    }
                    else//加急模式
                    {
                        if (TSPlist.Where(ph => ph.Priority == 1).ToList().Count != 0)
                        {
                            if (resultList.Count == 0 || VerticalLine.Count == 1)//强制输出的情况下照片第一章照片强制取照片
                            {
                                PhotoList = FindSuitPhotosForce(TSPlist.Where(ph => ph.Priority == 1).ToList(), currRectang, template.BarCodeStatus, out outHeightLeft, sourcePList, putPhotoOrder, template, CurentPrinter, MinX, CurrVLinePhotoPoint);
                            }
                            else//其他情况下 先加急的排如果木有 再按普通模式按普通模式
                            {
                                PhotoList = FindSuitPhotosForce(TSPlist.Where(ph => ph.Priority == 1).ToList(), currRectang, template.BarCodeStatus, out outHeightLeft, sourcePList, putPhotoOrder, template, CurentPrinter, MinX, CurrVLinePhotoPoint);

                                if (PhotoList == null || PhotoList.Count == 0)
                                {
                                    PhotoList = FindSuitPhotos21(TSPlist, currRectang, template.BarCodeStatus, out outHeightLeft, sourcePList, putPhotoOrder, template, CurentPrinter, MinX, autoModel, CurrVLinePhotoPoint);
                                }
                            }
                        }
                        else
                        {
                            PhotoList = FindSuitPhotos21(TSPlist, currRectang, template.BarCodeStatus, out outHeightLeft, sourcePList, putPhotoOrder, template, CurentPrinter, MinX, autoModel, CurrVLinePhotoPoint);
                        }

                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("取照片出错");
                }
                #endregion
               

                if (PhotoList == null || PhotoList.Count == 0)
                {
                   // WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "获取不到照片");
                    //try
                    //{
                    #region//找不到合适的照片来放
                    try
                    {
                        if (VerticalLine.Count == 1)
                        {
                            if (CutPhotoidList.Count == 0)
                            {
                                CanPutWaste = currWate;
                                CutPhotoidList = new List<int>();
                                foreach (PhotoInfo_PBDCT photoct in resultList)
                                {
                                    CutPhotoidList.Add(photoct.PhotoId);
                                }
                                currRectang.Height = currRectang.Width = 0;
                                break;//直接出因为没有照片了
                            }
                            if (VerticalLine.FirstOrDefault().StartPoint.X >= suitMinX)//已经达到出版最佳尺寸
                            {
                                break;
                            }
                            else//当不够设定的最佳尺寸时
                            {
                                ///1回滚
                                break;
                            }



                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("判断是否结束出错" + ex.InnerException);
                    }


                    //}
                    ///建立剩余空白版就是未找到的那块放入List
                    ///空白块分两种一种是横向一种是竖向实际是检查上下两个方向的x取小的
                    ///块的宽需要计算得出而不是当前操作快的宽度
                    //查找空白框的顶点所在的那个照片块
                    MyRectangle priRect = null;
                    int balckRWidth = 0;

                    //if (VerticalLine == null)
                    //{
                    //    throw new Exception("VerticalLine为空");
                    //}
                    

                    //重新写的填充小块的方法
                    Line firstLine = null;

                    try
                    {
                        firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                    }
                    catch (Exception ex) { new Exception("获取firstLine出错"); };
                    
                    if (firstLine == null)
                    {
                        if (TSPlist.Count == 0)
                        {
                            break;
                        }
                        // 理论不可以
                        //下一个轮回
                        sourcePList.ForEach(p => p.IsInBoard = false);

                        resultList = new List<PhotoInfo_PBDCT>();
                        VerticalLine = new List<Line>();
                        listRect = new List<MyRectangle>();
                        currRectang = new Rectangle(new Point(0, 0), new Size(boardLenght, boardHeight));//当前矩形框
                        continue;
                    }

                    
                    Point BlackLocation = firstLine.StartPoint;
                    int BlackH = firstLine.EndPoint.Y - firstLine.StartPoint.Y;
                    int TopLeft = 0, BottomLeft = 0;
                    //查找他的上面的矩形   下边Y值等于Location里的Y, X位于矩形内
                    MyRectangle TopRect = null;
                    try
                    {
                        TopRect = listRect.Where(r => r.PRactangle.Location.Y + r.PRactangle.Height == BlackLocation.Y && BlackLocation.X >= r.PRactangle.Location.X && BlackLocation.X < r.PRactangle.Location.X + r.PRactangle.Width).FirstOrDefault();
                        if (TopRect == null)
                        {
                            TopLeft = -1;//上面没有了
                        }
                        else
                        {
                            TopLeft = TopRect.PRactangle.Location.X + TopRect.PRactangle.Width - BlackLocation.X;
                        }

                        //查找他的下面的矩形   上边Y值等于firstLine终点的Y,   X位于矩形内
                        MyRectangle BottomRect = listRect.Where(r => r.PRactangle.Location.Y == firstLine.EndPoint.Y && firstLine.EndPoint.X >= r.PRactangle.Location.X && firstLine.EndPoint.X < r.PRactangle.Location.X + r.PRactangle.Width).FirstOrDefault();

                        if (BottomRect == null)
                        {
                            BottomLeft = -1;//下面没有了
                        }
                        else
                        {
                            BottomLeft = BottomRect.PRactangle.Location.X + BottomRect.PRactangle.Width - firstLine.EndPoint.X;
                        }
                        //计算要用空白补充的最小单位
                        if (TopLeft == -1 && BottomLeft != -1)
                        {
                            balckRWidth = BottomLeft;
                        }
                        else if (TopLeft != -1 && BottomLeft == -1)
                        {
                            balckRWidth = TopLeft;
                        }
                        else if (TopLeft != -1 && BottomLeft != -1)
                        {
                            if (TopLeft >= BottomLeft)
                            {
                                balckRWidth = BottomLeft;
                            }
                            else
                            {
                                balckRWidth = TopLeft;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("判断空白位置出错"+ex.InnerException.ToString());
                    }

                    try
                    {
                        MyRectangle BlackRect = new MyRectangle(new Rectangle(currRectang.Location, new Size(balckRWidth, currRectang.Size.Height)));
                        BlackRect.isUsefull = false;
                        if (BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width > maxWidth)
                        {
                            balckRWidth = maxWidth;
                        }
                        listRect.Add(BlackRect);//找不到合适的照片用白板代替应该控制白板的大小
                        //记录当前的竖线集合以便找到下一个矩形框
                        VerticalLine.Remove(VerticalLine.FirstOrDefault(p => p.StartPoint == BlackRect.PRactangle.Location));

                        Point startPoint = new Point(BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width, BlackRect.PRactangle.Y);
                        Point endPoint = new Point(BlackRect.PRactangle.Location.X + BlackRect.PRactangle.Width, BlackRect.PRactangle.Y + BlackRect.PRactangle.Height);


                        AddLine(ref VerticalLine, new Line(startPoint, endPoint));


                        //}

                        //将继续找下一个可以填充的块如果找不到情况重试
                        //获取最小的位置值XY 都是最小的
                        firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                        int minX = firstLine.StartPoint.X;
                        if (minX == 11998)
                        {

                        }
                        int minY = firstLine.StartPoint.Y;
                        if (firstLine.EndPoint.Y > boardHeight)
                        {

                        }
                        CurrVLinePhotoPoint = GetFocusPoint(listRect, firstLine);
                        int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                        int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                        currWidth = currRectang.Width;
                        Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));

                        currRectang = newrang;
                        continue;//继续填充
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("计算下一个矩形出错");
                    }

                    //}
                    #endregion
                    //}
                    //catch (Exception ex)
                    //{
                    //    throw new Exception("补空白出错" +ex.InnerException);
                    //}
                }
                else
                {
                    //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "获取到第一组照片");

                    if (listRect.Count == 0)//第一张照片
                    {
                        try
                        {
                            #region//第一张照片
                            // FristSize=PhotoList.FirstOrDefault().PSize;
                            MyRectangle photoRect = null;
                            CanPut = false;
                            bool isFirstPhoto = true;
                            int MaxWidth = 0;
                            foreach (PhotoInfo_PBDCT photo in PhotoList)
                            {
                                MyRectangle myr = new MyRectangle(photo);
                                myr.isUsefull = true;
                                listRect.Add(myr);
                                photo.IsInBoard = true;

                                //AddInBoardNum(sourcePList ,photo.OrderProductId);

                                resultList.Add(photo);
                                photoRect = myr;
                                //photo.TryTime++;
                                ////标记照片已经拼版了包含了副本
                                sourcePList.Where(xp => xp.PhotoId == photo.PhotoId).ToList().ForEach(p => p.IsInBoard = true);
                                //if (isFirstPhoto)
                                //{
                                //    photo.TryTime++;
                                //    isFirstPhoto = false;
                                //}

                                #region///验证用

                                #endregion
                                ////VerticalLine.Remove(VerticalLine.FirstOrDefault(p => p.StartPoint == currRectang.Location));

                                outCurrLength = myr.PRactangle.Location.X + myr.PRactangle.Width;//计算当前最长的数字
                                if (MaxWidth < outCurrLength)
                                {
                                    MaxWidth = outCurrLength;
                                }
                                Line delLine = VerticalLine.FirstOrDefault(p => p.StartPoint == myr.PRactangle.Location);
                                Point delStartPoint = new Point(0, 0);
                                Point delEndPoint = new Point(0, currRectang.Height);
                                if (delLine == null)
                                {

                                }
                                else
                                {
                                    delStartPoint = new Point(delLine.StartPoint.X, delLine.StartPoint.Y);
                                    delEndPoint = new Point(delLine.EndPoint.X, delLine.EndPoint.Y);
                                }
                                VerticalLine.Remove(delLine);//移除原先的线          
                                AddLine(ref VerticalLine, new Line(new Point(myr.PRactangle.X + myr.PRactangle.Width, myr.PRactangle.Y), new Point(myr.PRactangle.X + myr.PRactangle.Width, myr.PRactangle.Y + myr.PRactangle.Height)));
                                // VerticalLine.Add();
                                AddLine(ref VerticalLine, new Line(new Point(delStartPoint.X, myr.PRactangle.Y + myr.PRactangle.Height), new Point(myr.PRactangle.X, delEndPoint.Y)));
                                //VerticalLine.Add();
                                //outCurrLength = VerticalLine.Max(p => p.StartPoint.X);
                            }




                            Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                            if (firstLine.EndPoint.Y > boardHeight)
                            {

                            }
                            CurrVLinePhotoPoint = GetFocusPoint(listRect, firstLine);
                            int minX = firstLine.StartPoint.X;
                            int minY = firstLine.StartPoint.Y;
                            int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                            int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                            currWidth = currRectang.Width;
                            Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));
                            currRectang = newrang;
                            currWate = CheckWaste(listRect, boardHeight);
                            if (CanPutWaste >= currWate && outCurrLength >= MinX)
                            {
                                CanPutWaste = currWate;
                                CutPhotoidList = new List<int>();
                                foreach (PhotoInfo_PBDCT photoct in resultList)
                                {
                                    CutPhotoidList.Add(photoct.PhotoId);
                                }
                                MyRectangle topRect = listRect.Where(r => r.PRactangle.Y + r.PRactangle.Height == firstLine.StartPoint.Y && firstLine.StartPoint.X >= r.PRactangle.X && firstLine.StartPoint.X <= r.PRactangle.X + r.PRactangle.Width).OrderByDescending(o => o.PRactangle.X + o.PRactangle.Width).FirstOrDefault();
                                MyRectangle bottomRect = listRect.Where(r => r.PRactangle.Y == firstLine.EndPoint.Y && firstLine.EndPoint.X >= r.PRactangle.X && firstLine.EndPoint.X <= r.PRactangle.X + r.PRactangle.Width).OrderByDescending(o => o.PRactangle.X + o.PRactangle.Width).FirstOrDefault(); ;

                                if (topRect != null && bottomRect != null)
                                {
                                    int topL = topRect.PRactangle.X + topRect.PRactangle.Width - firstLine.StartPoint.X;
                                    int bottomL = bottomRect.PRactangle.X + bottomRect.PRactangle.Width - firstLine.StartPoint.X;
                                    if (topL >= bottomL)
                                    {
                                        CutRectangle = new Rectangle(new Point(minX, minY), new Size(topL, Height));

                                    }
                                    else
                                    {
                                        CutRectangle = new Rectangle(new Point(minX, minY), new Size(bottomL, Height));
                                    }
                                }
                                else if (topRect == null && bottomRect != null)
                                {
                                    int bottomL = bottomRect.PRactangle.X + bottomRect.PRactangle.Width - firstLine.StartPoint.X;
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(bottomL, Height));
                                }
                                else if (topRect != null && bottomRect == null)
                                {
                                    int topL = topRect.PRactangle.X + topRect.PRactangle.Width - firstLine.StartPoint.X;
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(topL, Height));
                                }
                                else
                                {
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(0, 0));
                                }
                            }
                            continue;//继续填充


                            #endregion
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("放第一批照片出错" + ex.StackTrace);
                        }
                    }
                    else
                    {
                        //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "获取到版上非第一组照片照片");

                        try
                        {
                            #region//将照片放到找到的合适的版上
                            int tHeight = 0;
                            //MyRectangle lastRang = null;
                            ///把照片放入找到的暂存块
                            MyRectangle photoRect = null;
                            foreach (PhotoInfo_PBDCT photo in PhotoList)
                            {
                                MyRectangle myr = new MyRectangle(photo);
                                myr.isUsefull = true;
                                listRect.Add(myr);//加入暂存块
                                //photo.IsInBoard = true;
                                resultList.Add(photo);//加入结果列表
                                //lastRang = myr;
                                photoRect = myr;
                                //标记照片已经拼版了包含了副本
                                sourcePList.Where(xp => xp.PhotoId == photo.PhotoId).ToList().ForEach(p => p.IsInBoard = true);
                                //tempHeight+=myr.PRactangle.Height;


                                //实际照片块


                                var templist = VerticalLine.Where(p => p.StartPoint.X == myr.PRactangle.Location.X && p.StartPoint.Y <= myr.PRactangle.Location.Y && p.EndPoint.Y >= myr.PRactangle.Location.Y);
                                if (templist.Count() == 0)
                                {

                                }
                                Line delLine = templist.FirstOrDefault();
                                if (delLine.StartPoint.Y == myr.PRactangle.Y)
                                {
                                    if (delLine == null)
                                    {
                                    }
                                    Line newLine = new Line(new Point(myr.PRactangle.Location.X, myr.PRactangle.Location.Y), new Point(myr.PRactangle.Location.X, myr.PRactangle.Location.Y + myr.PRactangle.Height));//照片所在左边的
                                    if (newLine == null)
                                    {

                                    }
                                    List<Line> lines = delLine.SubtractLine(newLine);
                                    if (lines.Count > 1)
                                    {

                                    }
                                    Point delStartPoint = new Point(delLine.StartPoint.X, delLine.StartPoint.Y);
                                    Point delEndPoint = new Point(delLine.EndPoint.X, delLine.EndPoint.Y);
                                    VerticalLine.Remove(delLine);//移除原先的线                      
                                    AddLine(ref VerticalLine, new Line(new Point(myr.PRactangle.Location.X + myr.PRactangle.Width, myr.PRactangle.Location.Y), new Point(myr.PRactangle.Location.X + myr.PRactangle.Width, myr.PRactangle.Location.Y + myr.PRactangle.Height)));//画照片右边的线

                                    //AddLine(ref VerticalLine, new Line(new Point(myr.PRactangle.Location.X, myr.PRactangle.Location.Y + myr.PRactangle.Height), new Point(delEndPoint.X, delEndPoint.Y)));//画照片下面左边的线
                                    foreach (Line l in lines)
                                    {
                                        AddLine(ref VerticalLine, l);
                                    }
                                }
                                else
                                {
                                    VerticalLine.Remove(delLine);//移除原先的线 
                                    AddLine(ref VerticalLine, new Line(new Point(delLine.StartPoint.X, delLine.StartPoint.Y), new Point(myr.PRactangle.X, myr.PRactangle.Y)));
                                    AddLine(ref VerticalLine, new Line(new Point(delLine.StartPoint.X + myr.PRactangle.Width, myr.PRactangle.Y), new Point(delLine.StartPoint.X + myr.PRactangle.Width, myr.PRactangle.Y + myr.PRactangle.Height)));
                                    AddLine(ref VerticalLine, new Line(new Point(myr.PRactangle.X, myr.PRactangle.Y + myr.PRactangle.Height), new Point(delLine.EndPoint.X, delLine.EndPoint.Y)));

                                }

                                //List<Line> lines=
                            }
                            Line lengthL = VerticalLine.OrderByDescending(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                            outCurrLength = lengthL.StartPoint.X;




                            //记录当前的竖线集合以便找到下一个矩形框
                            //获取最小的位置值XY 都是最小的
                            Line firstLine = VerticalLine.OrderBy(p => p.StartPoint, new MyPointComparer()).FirstOrDefault();
                            if (firstLine.EndPoint.Y > boardHeight)
                            {

                            }

                            CurrVLinePhotoPoint = GetFocusPoint(listRect, firstLine);

                            int minX = firstLine.StartPoint.X;
                            int minY = firstLine.StartPoint.Y;
                            int Height = firstLine.EndPoint.Y - firstLine.StartPoint.Y;//下个矩形的高等于线的高度
                            int Width = boardLenght - minX;//下个矩形的长度等于版长减去minx定点的横坐标
                            currWidth = currRectang.Width;
                            Rectangle newrang = new Rectangle(new Point(minX, minY), new Size(Width, Height));

                            currRectang = newrang;

                            ///当前放进去的浪费率
                            currWate = CheckWaste(listRect, boardHeight);
                            if (CanPutWaste >= currWate && outCurrLength >= MinX)
                            {
                                CanPutWaste = currWate;
                                CutPhotoidList = new List<int>();
                                foreach (PhotoInfo_PBDCT photoct in resultList)
                                {
                                    CutPhotoidList.Add(photoct.PhotoId);
                                }
                                MyRectangle topRect = listRect.Where(r => r.PRactangle.Y + r.PRactangle.Height == firstLine.StartPoint.Y && firstLine.StartPoint.X >= r.PRactangle.X && firstLine.StartPoint.X <= r.PRactangle.X + r.PRactangle.Width).OrderByDescending(o => o.PRactangle.X + o.PRactangle.Width).FirstOrDefault();
                                MyRectangle bottomRect = listRect.Where(r => r.PRactangle.Y == firstLine.EndPoint.Y && firstLine.EndPoint.X >= r.PRactangle.X && firstLine.EndPoint.X <= r.PRactangle.X + r.PRactangle.Width).OrderByDescending(o => o.PRactangle.X + o.PRactangle.Width).FirstOrDefault(); ;

                                if (topRect != null && bottomRect != null)
                                {
                                    int topL = topRect.PRactangle.X + topRect.PRactangle.Width - firstLine.StartPoint.X;
                                    int bottomL = bottomRect.PRactangle.X + bottomRect.PRactangle.Width - firstLine.StartPoint.X;
                                    if (topL >= bottomL)
                                    {
                                        CutRectangle = new Rectangle(new Point(minX, minY), new Size(topL, Height));

                                    }
                                    else
                                    {
                                        CutRectangle = new Rectangle(new Point(minX, minY), new Size(bottomL, Height));
                                    }
                                }
                                else if (topRect == null && bottomRect != null)
                                {
                                    int bottomL = bottomRect.PRactangle.X + bottomRect.PRactangle.Width - firstLine.StartPoint.X;
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(bottomL, Height));
                                }
                                else if (topRect != null && bottomRect == null)
                                {
                                    int topL = topRect.PRactangle.X + topRect.PRactangle.Width - firstLine.StartPoint.X;
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(topL, Height));
                                }
                                else
                                {
                                    CutRectangle = new Rectangle(new Point(minX, minY), new Size(0, 0));
                                }


                            }
                            continue;//继续填充
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("放置照片出错" + ex.StackTrace);
                        }

                            #endregion

                    }
                }

                //}
                //catch (Exception ex)
                //{
                //    throw new Exception("计算出错1");
                //}

            }

            if (currWate < 0)
            {
                currWate = 0.001;
            }

            outWaste = (decimal)currWate;

            List<PhotoInfo_PBDCT> rtnList = new List<PhotoInfo_PBDCT>();
           // WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "获取照片完毕");

            try
            {
                ///回滚获取照片列表
                int iCount = 0;
                //CutPhotoidList = new List<int>();
                foreach (PhotoInfo_PBDCT photo in resultList)
                {
                    if (CutPhotoidList.Contains(photo.PhotoId))
                    {

                        rtnList.Add(photo);
                        photo.IsInBoard = true;
                    }
                    else
                    {
                        photo.IsInBoard = false;
                        //photo.TryTime = 0;
                        //SubtractInBoardNum(sourcePList, photo.OrderProductId);
                    }
                    iCount++;
                }
                //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "正在回滚");

                //回滚结束后 填补回滚块
                List<PhotoInfo_PBDCT> PhotoBackList = new List<PhotoInfo_PBDCT>();
                int leftLength = 0;
                if (CutRectangle.Width != 0 || CutRectangle.Height != 0)
                {
                    PhotoBackList = FindSuitPhotosForce(sourcePList.Where(p => p.IsInBoard == false).ToList(), CutRectangle, template.BarCodeStatus, out leftLength, sourcePList, 0, template, CurentPrinter, 0, CurrVLinePhotoPoint);
                }
                if (PhotoBackList != null && PhotoBackList.Count != 0)
                {
                    foreach (PhotoInfo_PBDCT photo in PhotoBackList)
                    {
                        photo.IsInBoard = true;
                        rtnList.Add(photo);
                    }
                }

                ///计算版长
                if (rtnList.Count > 0)
                {
                    outCurrLength = rtnList.Max(p => p.Location.X + (p.IsWhirl ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth) + ((p.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft) ? BARCODEWIDTH : 0));
                }
                ///计算浪费率
                decimal photoAreas = 0;
                foreach (PhotoInfo_PBDCT photo in rtnList)
                {
                    if (photo.IsWhirl)
                    {
                        if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                        {
                             
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth) * (photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                        }
                        else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                        {
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH) * photo.Photo_Base_Info.PixFactHeigth;
                        }
                        else
                        {
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth) * (photo.Photo_Base_Info.PixFactHeigth);
                        }
                    }
                    else
                    {
                        if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                        {
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH) * photo.Photo_Base_Info.PixFactHeigth;
                        }
                        else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                        {
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth) * (photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                        }
                        else
                        {
                            photoAreas += (photo.Photo_Base_Info.PixFactWidth) * (photo.Photo_Base_Info.PixFactHeigth);
                        }
                    }

                }
                if (outCurrLength != 0)
                {
                    outWaste = 1 - photoAreas / ((decimal)outCurrLength * boardHeight);
                }
                else
                {
                    outWaste = 0;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("计算出错2");
            }


            //int maxY = rtnList.Max(p => p.Location.Y + (p.IsWhirl ? p.Photo_Base_Info.PixFactWidth : p.Photo_Base_Info.PixFactHeigth));
            //int maxy2 = rtnList.Max(p => p.Location.Y + (p.IsWhirl ? Convert.ToInt32(((double)p.Photo_Base_Info.PixWidth) / p.Photo_Base_Info.DPI * CurentPrinter.PDPI) : Convert.ToInt32(((double)p.Photo_Base_Info.PixHeight) / p.Photo_Base_Info.DPI * CurentPrinter.PDPI)));
            return rtnList;
        }

        List<Point> GetFocusPoint(List<MyRectangle> listRect, Line inLine)
        {
            List<MyRectangle> photoRLLeft = listRect.Where(p => (p.PRactangle.X + p.PRactangle.Width == inLine.StartPoint.X) && p.isUsefull).ToList();//线左侧的矩形
            List<MyRectangle> photoRLRight = listRect.Where(p => (p.PRactangle.X == inLine.StartPoint.X) && p.isUsefull).ToList();//线右侧的矩形

            //photoRL.ForEach(CurrVLinePhotoPoint.Add());
            List<Point> RtnPoint = new List<Point>();
            foreach (MyRectangle rect in photoRLLeft)//
            {
                //线左侧的矩形要加2个 点右上和右下 并且要判断点是否在线的Y范围内
                if (rect.PRactangle.Y >= inLine.StartPoint.Y && rect.PRactangle.Y <= inLine.EndPoint.Y)
                {
                    Point pointAdd = new Point(rect.PRactangle.X + rect.PRactangle.Width, rect.PRactangle.Y);
                    if (RtnPoint.Where(p => p.X == pointAdd.X && p.Y == pointAdd.Y).ToList().Count == 0)
                    {

                        RtnPoint.Add(pointAdd);
                    }

                }

                if (rect.PRactangle.Y + rect.PRactangle.Height <= inLine.EndPoint.Y && rect.PRactangle.Y + rect.PRactangle.Height >= inLine.StartPoint.Y)
                {
                    Point pointAdd = new Point(rect.PRactangle.X + rect.PRactangle.Width, rect.PRactangle.Y + rect.PRactangle.Height);
                    if (RtnPoint.Where(p => p.X == pointAdd.X && p.Y == pointAdd.Y).ToList().Count == 0)
                    {
                        RtnPoint.Add(pointAdd);
                    }
                }
                //CurrVLinePhotoPoint.Add(new Point(firstLine.StartPoint.X,rect.PRactangle.Y);

            }

            foreach (MyRectangle rect in photoRLRight)
            {
                //线右侧的点要加2个 左上和左下
                if (rect.PRactangle.Y >= inLine.StartPoint.Y)
                {
                    RtnPoint.Add(new Point(rect.PRactangle.X, rect.PRactangle.Y));
                }

                if (rect.PRactangle.Y + rect.PRactangle.Height <= inLine.EndPoint.Y)
                {
                    RtnPoint.Add(new Point(rect.PRactangle.X, rect.PRactangle.Y + rect.PRactangle.Height));
                }

            }
            return RtnPoint;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inList"></param>
        /// <param name="inMinX"></param>
        /// <param name="inWaste"></param>
        /// <returns></returns>
        bool CheckWasteAndLength(List<MyRectangle> inList, int inMinX, double inWaste, int boardHeight, out double currWaste)
        {
            if (inList == null || inList.Count == 0)
            {
                currWaste = 0;
                return false;
            }
            //double ALLAreas = inList.Sum(p => p.Areas);
            double UsefulAreas = inList.Where(w => w.isUsefull).Sum(p => p.Areas);
            int max = inList.Max(p => p.PRactangle.Location.X + p.PRactangle.Width);
            double ALLAreas = boardHeight * max;
            currWaste = 1 - UsefulAreas / ALLAreas;
            if ((currWaste <= inWaste) && max >= inMinX)
            {

                return true;

            }
            else
            {
                return false;
            }
        }
        bool CheckWaste(List<MyRectangle> inList, double inWaste, int boardHeight, out double outWaste)
        {
            if (inList == null || inList.Count == 0)
            {
                outWaste = 0;
                return false;
            }
            //double ALLAreas = inList.Sum(p => p.Areas);
            double UsefulAreas = inList.Where(w => w.isUsefull).Sum(p => p.Areas);
            //double UsefulAll = inList.Sum(p => p.Areas);
            int max = inList.Max(p => p.PRactangle.Location.X + p.PRactangle.Width);
            //max = inList.Max(p => p.Location.X + (p.IsWhirl ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth) + ((p.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft) ? BARCODEWIDTH : 0));
            double ALLAreas = boardHeight * max;
            double currWaste = 1 - UsefulAreas / ALLAreas;
            if ((currWaste <= inWaste))
            {
                outWaste = currWaste;
                return true;

            }
            else
            {
                outWaste = 0;
                return false;
            }
        }
        double CheckWaste(List<MyRectangle> inList, int boardHeight)
        {
            if (inList == null || inList.Count == 0)
            {

                return 0;
            }
            //double ALLAreas = inList.Sum(p => p.Areas);
            double UsefulAreas = inList.Where(w => w.isUsefull).Sum(p => p.Areas);
            //double UsefulAll = inList.Sum(p => p.Areas);
            int max = inList.Max(p => p.PRactangle.Location.X + p.PRactangle.Width);
            double ALLAreas = boardHeight * max;
            double currWaste = 1 - UsefulAreas / ALLAreas;


            return currWaste;


        }
        private List<PhotoInfo_PBDCT> ColnePhotoList(List<PhotoInfo_PBDCT> sourcePList)
        {
            List<PhotoInfo_PBDCT> newList = new List<PhotoInfo_PBDCT>();
            foreach (PhotoInfo_PBDCT photo in sourcePList)
            {
                photo.TryTime = 0;
                photo.IsInBoard = false;
                newList.Add(photo);
                PhotoInfo_PBDCT newPhoto = new PhotoInfo_PBDCT() { PhotoId = photo.PhotoId, IsCopy = true, Photo_Base_Info = new PhotoDCT() { PhotoId = photo.Photo_Base_Info.PhotoId, Width = photo.Photo_Base_Info.Height, Height = photo.Photo_Base_Info.Width, PixHeight = photo.Photo_Base_Info.PixWidth, PixWidth = photo.Photo_Base_Info.PixHeight, PixFactHeigth = photo.Photo_Base_Info.PixFactWidth, PixFactWidth = photo.Photo_Base_Info.PixFactHeigth }, IsWhirl = true, IsInBoard = false, TryTime = 0 };
                newList.Add(newPhoto);
            }
            return newList;

        }
        /// <summary>
        /// 全自动拼板
        /// </summary>
        /// <param name="sourcePList"></param>
        /// <param name="template"></param>
        /// <param name="CurentPrinter"></param>
        /// <returns></returns>
        public List<PhotoInfo_PBDCT> AutoPinS(List<PhotoInfo_PBDCT> sourcePList, BoardTemplate_DCT template, PrinterDCT CurentPrinter, int samlltype)
        {

            int boardMinX = 12;//打印机最小的出版长度               
            int bordHeight = Convert.ToInt32(template.Height * CurentPrinter.PDPI);
            int boardLenght = Convert.ToInt32(template.Length * CurentPrinter.PDPI);
            Point PLocation = new Point(0, 0);  //图片的坐标,以实际的大纸的坐标为准    
            Point Re_Point1 = new Point(0, 0);  //剩余空间的启始坐标    
            Size Re_Size1 = new Size(0, 0);   //剩余空间的SIZE    
            Point Re_Point2 = new Point(0, 0);  //剩余空间的启始坐标    
            Size Re_Size2 = new Size(0, 0);   //剩余空间的SIZE    
            List<PhotoInfo_PBDCT> resultList = new List<PhotoInfo_PBDCT>();

            #region 特殊的自动拼板

            List<PhotoInfo_PBDCT> tempList = sourcePList.Where(p => !p.IsAutoPB).ToList();
            if (tempList.Count < 3)
            {
                IsNewWhirl = true;
                foreach (var p in sourcePList)
                {
                    p.IsAutoPB = false;
                    p.IsInBoard = false;
                }
            }
            else
            {
                sourcePList = tempList;
            }


            List<Size> listS = sourcePList.Where(p => bordHeight % p.PSize.Width == 0 || bordHeight % p.PSize.Height == 0).Select(p => p.PSize).Distinct(new MySizeComparer()).ToList();

            List<PhotoInfo_PBDCT> someSizePList = new List<PhotoInfo_PBDCT>();
            foreach (Size s in listS)
            {
                int row_colum = 0;//每一列有最多最放多少张图片
                int fact_colum = 0;//此Size的图片实际可以放多少列

                Size sT = s;

                if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    sT.Height += BARCODEWIDTH;
                }
                else if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    sT.Width += BARCODEWIDTH;
                }

                if (bordHeight % sT.Height == 0)
                {
                    row_colum = bordHeight / sT.Height;
                }
                else
                {
                    if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                    {
                        sT.Width += BARCODEWIDTH;
                    }
                    else if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                    {
                        sT.Width -= BARCODEWIDTH;
                    }

                    if (bordHeight % sT.Width == 0)
                    {
                        row_colum = bordHeight / sT.Width;

                        if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                        {
                            sT.Width = s.Height - BARCODEWIDTH;
                            sT.Height = sT.Width;
                        }
                        else if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                        {
                            sT.Width = s.Height + BARCODEWIDTH;
                            sT.Height = s.Width - BARCODEWIDTH;
                        }
                        else
                        {
                            sT.Width = s.Height;
                            sT.Height = s.Width;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }


                //sT.Width += BARCODEWIDTH;
                //if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                //{
                //    someSizePList = sourcePList.Where(p => (!p.IsInBoard && (p.Photo_Base_Info.PixHeight == sT.Height && p.Photo_Base_Info.PixWidth == sT.Width - BARCODEWIDTH) || sT.Width - BARCODEWIDTH == p.Photo_Base_Info.PixHeight && sT.Height == p.Photo_Base_Info.PixWidth)).Distinct(new MyPhotoComparer()).ToList();
                //}
                //else if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                //{
                //    someSizePList = sourcePList.Where(p => (!p.IsInBoard && (p.Photo_Base_Info.PixHeight == sT.Height - BARCODEWIDTH && p.Photo_Base_Info.PixWidth == sT.Width) || sT.Width == p.Photo_Base_Info.PixHeight && sT.Height - BARCODEWIDTH == p.Photo_Base_Info.PixWidth)).Distinct(new MyPhotoComparer()).ToList();
                //}
                //else//无条码
                //{
                //    someSizePList = sourcePList.Where(p => (!p.IsInBoard && (p.Photo_Base_Info.PixHeight == sT.Height && p.Photo_Base_Info.PixWidth == sT.Width) || sT.Width == p.Photo_Base_Info.PixHeight && sT.Height == p.Photo_Base_Info.PixWidth)).Distinct(new MyPhotoComparer()).ToList();
                //}
                someSizePList = sourcePList.Where(p => (!p.IsInBoard && (p.Photo_Base_Info.PixHeight == sT.Height && p.Photo_Base_Info.PixWidth == sT.Width) || sT.Width == p.Photo_Base_Info.PixHeight && sT.Height == p.Photo_Base_Info.PixWidth)).Distinct(new MyPhotoComparer()).ToList();



                int pNum = someSizePList.Count;//此种尺寸的图片总数
                if (pNum >= row_colum)
                {
                    int fact_columT = pNum / row_colum;
                    fact_colum = boardLenght / sT.Width;
                    if (fact_columT < fact_colum)
                    {
                        fact_colum = fact_columT;
                    }
                    for (int colum = 0; colum < fact_colum; colum++)
                    {
                        if (PLocation.X + sT.Width > boardLenght)
                        {
                            break;
                        }

                        for (int pic = 0; pic < row_colum; pic++)
                        {
                            PhotoInfo_PBDCT p = someSizePList.OrderByDescending(pt => pt.Priority).FirstOrDefault(pt => !pt.IsInBoard);
                            if (p == null)
                            {
                                break;
                            }
                            if (template.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)//条码在左右
                            {
                                if (p.Photo_Base_Info.PixHeight == sT.Width - BARCODEWIDTH)
                                {
                                    p.IsWhirl = true;
                                }
                            }
                            else//无条码或是条码在下面
                            {
                                if (p.Photo_Base_Info.PixWidth == sT.Height - BARCODEWIDTH)
                                {
                                    p.IsWhirl = true;
                                }
                            }

                            p.IsInBoard = true;
                            p.IsAutoPB = true;
                            p.BarCodeStatus = template.BarCodeStatus;
                            p.Location = PLocation;

                            PLocation.Y += sT.Height;

                            resultList.Add(p);
                        }
                        PLocation.X += sT.Width;//换列时
                        PLocation.Y = 0;//换列时X=0
                    }
                }
            }
            #endregion

            if (resultList.Count < 1)//启用普通的拼板方法
            {
                #region 正常的自动拼板

                PLocation = new Point(0, 0);
                Size source_Size = new Size(boardLenght, bordHeight);

                Re_AutoPB(ref sourcePList, PLocation, source_Size, ref resultList, template.BarCodeStatus);


                #endregion
            }
            else
            {

                //已拼好的版的长度是否符合要求
                if (resultList.Max(p => p.Location.X + (p.IsWhirl ? p.Photo_Base_Info.PixHeight : p.Photo_Base_Info.PixWidth)) < boardMinX)
                {
                    foreach (var p in resultList)
                    {
                        p.IsWhirl = false;
                        p.IsInBoard = false;
                        p.Location = new Point(0, 0);
                    }
                    resultList.Clear();
                    PLocation = new Point(0, 0);
                    Size source_Size = new Size(boardLenght, bordHeight);
                    Re_AutoPB(ref sourcePList, PLocation, source_Size, ref resultList, template.BarCodeStatus);
                }
            }

            return resultList;
        }
        
        public List<PhotoInfo_PBDCT> AutoPinSN(List<List<PhotoInfo_PBDCT>> list, BoardTemplate_DCT template, PrinterDCT CurentPrinter, decimal waste, out decimal outWaste, out int length, int smalltype)
        {
            List<PhotoInfo_PBDCT> sourcePList = new List<PhotoInfo_PBDCT>();
            foreach (var item in list)
            {
                sourcePList.AddRange(item.Where(p => !p.IsInBoard).ToList());
            }

            return AutoPinSN21(sourcePList, template, CurentPrinter, waste, out outWaste, out length, smalltype, true, 0);
        }
        public List<PhotoInfo_PBDCT> AutoPinSN2(List<List<PhotoInfo_PBDCT>> list, BoardTemplate_DCT template, PrinterDCT CurentPrinter, decimal waste, out decimal outWaste, out int length, int minLength, bool isForcePrint, int level)
        {
            List<PhotoInfo_PBDCT> sourcePList = new List<PhotoInfo_PBDCT>();
            foreach (var item in list)
            {
                sourcePList.AddRange(item.Where(p => !p.IsInBoard).ToList());
            }

            return AutoPinSN21(sourcePList, template, CurentPrinter, waste, out outWaste, out length, minLength, isForcePrint, level);
        }
        public List<PhotoInfo_PBDCT> AutoPinSN2(List<List<PhotoInfo_PBDCT>> list, List<BoardTemplate_DCT> templateList, PrinterDCT CurentPrinter, decimal waste, out decimal outWaste, out int length, int minLength, bool isForcePrint, int level,out BoardTemplate_DCT outBoardTemplete)
        {
            List<PhotoInfo_PBDCT> rtnList = new List<PhotoInfo_PBDCT>();
            List<TempResult> resultList = new List<TempResult>();
            List<PhotoInfo_PBDCT> sourcePList = new List<PhotoInfo_PBDCT>();
            foreach (var item in list)
            {
                sourcePList.AddRange(item.Where(p => !p.IsInBoard).ToList());
            }
            List<PhotoInfo_PBDCT> inputPList = new List<PhotoInfo_PBDCT>();
            //sourcePList.ForEach(inputPList.Add(new PhotoInfo_PBDCT()));
        

            //获取最大的版面
            templateList = templateList.OrderByDescending(p => p.Height).ToList();//版面从大到小获取
            BoardTemplate_DCT firstTemp = templateList.FirstOrDefault();
           
            inputPList = new List<PhotoInfo_PBDCT>();
            foreach (PhotoInfo_PBDCT ppb in sourcePList)
            {
                PhotoInfo_PBDCT photo = new PhotoInfo_PBDCT();
                photo.Photo_Base_Info = new PhotoDCT() { PhotoId = ppb.Photo_Base_Info.PhotoId, PaperFaceId = ppb.Photo_Base_Info.PaperFaceId, PhotoName = ppb.Photo_Base_Info.PhotoName, PixHeight = ppb.Photo_Base_Info.PixHeight, PixWidth = ppb.Photo_Base_Info.PixWidth, DPI = ppb.Photo_Base_Info.DPI };
                photo.IsInBoard = false;
                photo.IsWhirl = false;
                photo.BarCodeStatus = Status.PhotoBarCodeStatus.NoBarCode;
                inputPList.Add(photo);
            }
            //最大的版面获取到照片
            List<PhotoInfo_PBDCT> rtnFrist = AutoPinSN21(inputPList, firstTemp, CurentPrinter, waste, out outWaste, out length, minLength, isForcePrint, level);
            TempResult firstR = new TempResult();
            firstR.Length = length;
            firstR.Waste = outWaste;
            firstR.BoardTemplete = firstTemp;
            firstR.PhotoList = rtnFrist;

            resultList.Add(firstR);
            if (templateList.Count<= 1)
            {
                //resultList.Add(firstR);
            }
            else
            {
                //用最大的版面取得照片集合在剩下的版面中顺序执行
                for (int i = 1; i < templateList.Count; i++)// (BoardTemplate_DCT template in templateList)
                {
                    BoardTemplate_DCT template = templateList[i];
                    List<PhotoInfo_PBDCT> inPList = new List<PhotoInfo_PBDCT>();
                    foreach (PhotoInfo_PBDCT ppb in rtnFrist)
                    {
                        PhotoInfo_PBDCT photo = new PhotoInfo_PBDCT();
                        photo.Photo_Base_Info = new PhotoDCT() { PhotoId = ppb.Photo_Base_Info.PhotoId, PaperFaceId = ppb.Photo_Base_Info.PaperFaceId, PhotoName = ppb.Photo_Base_Info.PhotoName, PixHeight = ppb.Photo_Base_Info.PixHeight, PixWidth = ppb.Photo_Base_Info.PixWidth, DPI = ppb.Photo_Base_Info.DPI };
                        photo.IsInBoard = false;
                        photo.IsWhirl = false;
                        photo.BarCodeStatus = Status.PhotoBarCodeStatus.NoBarCode;
                        photo.Location = new Point(0, 0);
                        inPList.Add(photo);
                    }
                    List<PhotoInfo_PBDCT> rtn = AutoPinSN21(inPList, template, CurentPrinter, waste, out outWaste, out length, minLength, isForcePrint, level);
                    TempResult tResult = new TempResult();
                    tResult.Length = length;
                    tResult.Waste = outWaste;
                    tResult.BoardTemplete = template;

                    tResult.PhotoList = rtn;
                    if (length > minLength / 100.0 * template.Length * CurentPrinter.PDPI)
                    {
                        resultList.Add(tResult);
                    }
                }
            }
            
            resultList.RemoveAll(p => p.PhotoList.Count == 0);
            resultList = resultList.OrderBy(p=>p.Waste).ToList();

            if (resultList.Count > 0)
            {
                outWaste = resultList.FirstOrDefault().Waste;
                length = resultList.FirstOrDefault().Length;

                List<PhotoInfo_PBDCT> tempList = resultList.FirstOrDefault(p=>p.PhotoList.Count>0).PhotoList;
                foreach (PhotoInfo_PBDCT pdc in tempList)
                {
                    PhotoInfo_PBDCT real = sourcePList.FirstOrDefault(p => p.Photo_Base_Info.PhotoId == pdc.Photo_Base_Info.PhotoId);
                    real.Location = pdc.Location;
                    real.IsInBoard = pdc.IsInBoard;
                    real.BarCodeStatus = pdc.BarCodeStatus;
                    real.IsWhirl = pdc.IsWhirl;
                    rtnList.Add(real);
                }
                outBoardTemplete = resultList.FirstOrDefault().BoardTemplete;
                
            }
            else
            {
                outBoardTemplete = templateList.FirstOrDefault();
                outWaste = 0;
                length = 0;
                rtnList = null;

            }
            return rtnList;
            //return AutoPinSN21(inputPList, templateList[0], CurentPrinter, waste, out outWaste, out length, minLength, isForcePrint, level);
        }
        public List<PhotoInfo_PBDCT> AutoPinS(List<List<PhotoInfo_PBDCT>> list, BoardTemplate_DCT template, PrinterDCT CurentPrinter, int smalltype)
        {
            List<PhotoInfo_PBDCT> sourcePList = new List<PhotoInfo_PBDCT>();
            foreach (var item in list)
            {
                sourcePList.AddRange(item.Where(p => !p.IsInBoard).ToList());
            }

            return AutoPinS(sourcePList, template, CurentPrinter, smalltype);
        }
        private void ReNew_AutoPB(ref List<PhotoInfo_PBDCT> sourPList, Point r_StartPoint, Size r_Size, ref List<PhotoInfo_PBDCT> resultPList, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus)
        {

            Point XDstart_Point = new Point(0, 0);      //待拼板区域的相对坐标
            List<PhotoInfo_PBDCT> TSPlist;             //TSPlist:Temp Source Picture List
            Point re_Point1 = new Point(0, 0);          //剩余空间1的起始坐标
            Size re_Size1 = new Size(0, 0);             //剩余空间1的SIZE

            Point re_Point2 = new Point(0, 0);          //剩余空间2的起始坐标
            Size re_Size2 = new Size(0, 0);             //剩余空间2的SIZE

            Point re_Point3 = new Point(0, 0);          //剩余空间3的起始坐标
            Size re_Size3 = new Size(0, 0);             //剩余空间3的SIZE

            TSPlist = sourPList.Where(p => !p.IsInBoard).OrderByDescending(p => p.Photo_Base_Info.PixHeight).ToList();//按高度从大到小排序
            if (TSPlist == null || TSPlist.Count < 1)
            {
                return;
            }


            //一张刚好可以装满该区域
            // List<PhotoInfo_PBDCT> photoList = TSPlist.Where(p => p.Photo_Base_Info.Height == r_Size.Height && p.Photo_Base_Info.Width < r_Size.Width).OrderByDescending(order => order.Photo_Base_Info.Width).ToList();
            //if (photoList.Count > 0)
            //{
            //    PhotoInfo_PBDCT currPhoto = photoList.FirstOrDefault();
            //    PhotoInfo_PBDCT truePhoto=new PhotoInfo_PBDCT();
            //    if (currPhoto.IsCopy)
            //    {
            //        truePhoto = photoList.FirstOrDefault(p => p.PhotoId == currPhoto.PhotoId && p.IsCopy == false);
            //        truePhoto.IsWhirl = true;
            //    }
            //    else
            //    {
            //        truePhoto = currPhoto;

            //    }

            //    currPhoto.Location = truePhoto.Location = new Point(r_StartPoint.X + XDstart_Point.X, r_StartPoint.Y + XDstart_Point.Y);
            //    currPhoto.BarCodeStatus=truePhoto.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft;
            //    currPhoto.IsAutoPB=truePhoto.IsAutoPB = true;
            //    currPhoto.IsInBoard=truePhoto.IsInBoard = true;
            //   //计算剩余空间 size1 size2=size3=null
            //    re_Point1=


            //}
            //MyRectangle rect = new MyRectangle();
            //List<MyRectangle> listRect = new List<MyRectangle>();
            //while (true)
            //{
            //    List<PhotoInfo_PBDCT> =FindSuitPhotos(TSPlist,

            //}
        }
        List<PhotoInfo_PBDCT> FindSuitPhotos(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, int samlltype)
        {
            if (inList.Count == 1)
            {
                //LeftH = currRang.Height - inList.FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                //return inList;
            }
            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var photoSizeGroup = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth).GroupBy(p => p.PhotoSize);
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            List<PhotoInfo_PBDCT> suitest = null;
            int Min_Suit = inHeigth;//最小匹配度
            int PhotoNum = 0;
            var Group = photoSizeGroup.OrderByDescending(order => order.Key, new MyStringComparer());
            foreach (var pzgN in Group)//尺寸从大到小尝试 最优
            {
                //pzg = pzg.Distinct(new MyPhotoComparer()).ToList(); ;
                var pzg = pzgN.ToList();
                pzg = pzg.Distinct(new MyPhotoComparer()).ToList();
                pzg.OrderBy(order => order.OrderProductId);
                int colum_row = 0;//可以放置的数量
                int suit_Num = 0;//照片片匹配度 越小越好
                int currPhotoCount = pzg.ToList().Count();
                if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)//上下打条码的时候
                {
                    suit_Num = inHeigth % (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                    colum_row = inHeigth / (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth);
                    int suitNoBarcode = inHeigth % (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth);
                    //colum_row = inHeigth / (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth);

                    if (suitNoBarcode == 0)//虽然条码要上下放但是照片能够正好放下 吧条码放置在左右去
                    {
                        if (colum_row <= currPhotoCount)//数量足够
                        {
                            suitest = pzg.ToList();//尺寸刚好就把条码放置在左右两侧
                            suitest.ForEach(p => p.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);
                            //return suitest;
                            PhotoNum = colum_row;
                            break;
                        }
                        else
                        {
                            //suitest.ForEach(p => p.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                            suit_Num = (colum_row - currPhotoCount) * pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + suit_Num;
                        }
                    }
                    else
                    {
                        suit_Num = inHeigth % (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                        colum_row = inHeigth / (pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                        //suitest.ForEach(p => p.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suit_Num = inHeigth % pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                    colum_row = inHeigth / pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                    //suitest.ForEach(p => p.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);
                    //if (suit_Num == 0)
                    //{
                    //    suitest = pzg.ToList();//
                    //    suitest.ForEach(p => p.BarCodeStatus = barCodeStatus);
                    //    return suitest;
                    //}
                    //else
                    //{ }

                }
                if (samlltype == 0)//不限制
                {

                }
                else if (samlltype == 1)//5寸
                {
                    if (colum_row > 4)//这样的照片定义为小照片 为后面补空用
                    {
                        continue;
                    }
                }
                else if (samlltype == 2)//3寸
                {
                    if (colum_row > 6)//这样的照片定义为小照片 为后面补空用
                    {
                        continue;
                    }
                }
                else
                {

                }

                if (colum_row <= currPhotoCount)//该组照片数量够
                {
                    //if ((pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + BARCODEWIDTH) * colum_row > currRang.Height)
                    //{
                    //    continue;
                    //}
                    if (Min_Suit > suit_Num)//比较适配度取最合适
                    {
                        Min_Suit = suit_Num;
                        PhotoNum = colum_row;
                        suitest = pzg.ToList();
                        suitest.ForEach(p => p.BarCodeStatus = barCodeStatus);
                    }
                    else
                    {

                    }
                }
                else
                {
                    int shortNum = colum_row - currPhotoCount;
                    int currGHeight = pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                    //if ((pzg.FirstOrDefault().Photo_Base_Info.PixFactHeigth + BARCODEWIDTH) * pzg.Count > currRang.Height)
                    //{
                    //    continue;
                    //}
                    if (Min_Suit > suit_Num + currGHeight * shortNum)
                    {

                        Min_Suit = suit_Num + currGHeight * shortNum;
                        PhotoNum = currPhotoCount;
                        suitest = pzg.ToList();
                        //suitest = pzg.ToList().Distinct(new MyPhotoComparer()).ToList();
                        suitest.ForEach(p => p.BarCodeStatus = barCodeStatus);

                    }

                }
            }
            if (suitest != null)
            {
                suitest = suitest.OrderByDescending(p => p.Priority).Take(PhotoNum).ToList();
            }
            else
            {
                LeftH = currRang.Height;
                return null;
            }

            ///求和计算最合适的
            inList = inList.OrderByDescending(or => or.PixHeight).ToList();
            ////for (int i = 0; i < inList.Count(); i++)
            ////{
            ////    for(int j=0;j)
            ////}


            int col = 0;
            foreach (var photo in suitest)
            {
                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    photo.Location = new Point(currRang.Location.X, currRang.Location.Y + col * (photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH));
                    col++;
                }
                else
                {
                    photo.Location = new Point(currRang.Location.X, currRang.Location.Y + col * photo.Photo_Base_Info.PixFactHeigth);
                    col++;
                }

                ////计算合适的 照片的坐标
                //if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                //{
                //    currPosition += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                //    photo.Location = new Point(currRang.Location.X, currPosition);
                //    col++;
                //}
                //else
                //{
                //    currPosition += photo.Photo_Base_Info.PixFactHeigth;
                //    photo.Location = new Point(currRang.Location.X, currPosition);
                //    col++;
                //}
            }
            LeftH = Min_Suit;//还差多少可以填满
            return suitest;

        }

        List<PhotoInfo_PBDCT> FindSuitPhotos2(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, int putPhotoOrder)
        {

            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            //var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth).OrderBy(or => or.Photo_Base_Info.PixFactHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).OrderByDescending(or => or.Photo_Base_Info.PixFactHeigth).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.FirstOrDefault().Photo_Base_Info.PixFactHeigth).ToList();

            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }
            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;
            int barcodeWidth = 30;
            if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
            {
                barcodeWidth = 0;
            }
            sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, barcodeWidth, BlackWidthLong[changeLevel], BlackWidthLong[changeLevel], BlackWidthLong[changeLevel]);//

            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }

            iMageInfo[] arrResult = (iMageInfo[])sizeResult;
            if (arrResult[0].Count == 0)
            {
                LeftH = 0;
                return new List<PhotoInfo_PBDCT>();
            }
            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();

            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                //处理条码
                if (barCodeStatus != SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
                {
                    if (arrResult[i].barCode == 0)
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                    }
                    else
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode);
                }
                //处理旋转
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                else
                {
                    suitTemp.ForEach(en => en.IsWhirl = false);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片



            //确定照片的摆放顺序
            //Point leftTop = currRang.Location;//左上定点
            //Point leftBottom = new Point(currRang.Location.X, currRang.Location.Y + currRang.Height);//坐下顶点



            //suitest.Add(photoSizeGroup.FirstOrDefault());
            //suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            if (putPhotoOrder == 0)
            {
                suitest = suitest.OrderBy(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            else
            {
                suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            int col = 0;
            int CurrHeight = currRang.Location.Y;
            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                //if (photo.Location.X == 0 && photo.Location.Y == 0)
                //{

                //}

            }
            LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满

            if (LeftH <= 0)
            {

            }
            else
            {
                //if (putPhotoOrder == 0)
                //{ //suitest.ForEach(p => p.Location = new Point(p.Location.X, p.Location.Y + LeftH));
                //    foreach (var photo in suitest)
                //    {
                //        photo.Location = new Point(photo.Location.X, photo.Location.Y + LeftH);

                //    }
                //}
                //else
                //{

                //}
            }
            return suitest;

        }
        List<PhotoInfo_PBDCT> FindSuitPhotos3(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, List<MyRectangle> listRect)
        {



            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.Key, new MyStringComparer()).ToList();
            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }


            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;

            sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, 30, BlackWidth[changeLevel], ComboxNum[changeLevel], CountLevel[changeLevel]);//


            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }
            iMageInfo[] arrResult = (iMageInfo[])sizeResult;
            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();

            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                if (arrResult[i].barCode == 0)
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                }
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片


            //suitest.Add(photoSizeGroup.FirstOrDefault());
            suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            int col = 0;
            int CurrHeight = currRang.Location.Y;
            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }


            }
            LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满
            if (LeftH < 0)
            {

            }
            return suitest;

        }

        List<PhotoInfo_PBDCT> FindSuitPhotos4(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, int putPhotoOrder)
        {

            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.Key, new MyStringComparerHeight()).ToList();
            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }
            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;
            int barcodeWidth = 30;
            if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
            {
                barcodeWidth = 0;
            }
            sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, barcodeWidth, BlackWidth[changeLevel], ComboxNum[changeLevel], CountLevel[changeLevel]);//

            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }
            iMageInfo[] arrResult = (iMageInfo[])sizeResult;
            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();

            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                //处理条码
                if (barCodeStatus != SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
                {
                    if (arrResult[i].barCode == 0)
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                    }
                    else
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode);
                }
                //处理旋转
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                else
                {
                    suitTemp.ForEach(en => en.IsWhirl = false);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片



            //确定照片的摆放顺序
            //Point leftTop = currRang.Location;//左上定点
            //Point leftBottom = new Point(currRang.Location.X, currRang.Location.Y + currRang.Height);//坐下顶点



            //suitest.Add(photoSizeGroup.FirstOrDefault());
            //suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            if (putPhotoOrder == 0)
            {
                suitest = suitest.OrderBy(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            else
            {
                suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            int col = 0;
            int CurrHeight = currRang.Location.Y;
            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }


            }
            LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满

            if (LeftH <= 0)
            {

            }
            else
            {
                //int boardHeight = Convert.ToInt32(template.Height * CurentPrinter.PDPI);
                if (putPhotoOrder == 0)
                { //suitest.ForEach(p => p.Location = new Point(p.Location.X, p.Location.Y + LeftH));
                    foreach (var photo in suitest)
                    {
                        photo.Location = new Point(photo.Location.X, photo.Location.Y + LeftH);

                    }
                }
                else
                {

                }

            }

            return suitest;

        }
        /// <summary>
        /// 获取一组照片
        /// </summary>
        /// <param name="inList">可用照片列表</param>
        /// <param name="currRang">当前填充矩形框</param>
        /// <param name="barCodeStatus">条码放置方向</param>
        /// <param name="LeftH">剩余高度</param>
        /// <param name="AllList"></param>
        /// <param name="putPhotoOrder">照片放置宽度顺序</param>
        /// <param name="template">当前模板</param>
        /// <param name="CurentPrinter">当前打印机</param>
        /// <param name="MinLength">最小版长</param>
        /// <param name="autoModel">拼版模式</param>
        /// /// <param name="inVPoint">当前竖线上的照片点</param>
        /// <returns></returns>
        List<PhotoInfo_PBDCT> FindSuitPhotos21(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, int putPhotoOrder, BoardTemplate_DCT template, PrinterDCT CurentPrinter, int MinLength, int aotuModel, List<Point> inVPoint)
        {

            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            //var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth).OrderBy(or => or.Photo_Base_Info.PixFactHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).OrderByDescending(or => or.Photo_Base_Info.PixFactHeigth).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.FirstOrDefault().Photo_Base_Info.PixFactHeigth).ToList();

            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }
            int[] UseBlackWidth = new int[3];
            int[] UseComboxNum = new int[3];
            int[] UseCountLevel = new int[3];
            if (aotuModel == 0)
            {
                UseBlackWidth = BlackWidthLong;
                UseComboxNum = ComboxNumLong;
                UseCountLevel = CountLevelLong;
            }
            else if (aotuModel == 1)
            {
                UseBlackWidth = BlackWidthLowWaste;
                UseComboxNum = BlackWidthLowWaste;
                UseCountLevel = BlackWidthLowWaste;
            }
            else
            {
                UseBlackWidth = BlackWidthLong;
                UseComboxNum = ComboxNumLong;
                UseCountLevel = CountLevelLong;
            }
            //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "源有"+sizeArray+"组照片");
            //foreach (var aa in sizeArray)
            //{
            //    int bb = 1;
            //    WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "aa=" + " " + aa.Width + " " + aa.Height + " " );
            //    bb++;
            //}
            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;
            int barcodeWidth = 30;
            if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
            {
                barcodeWidth = 0;
            }
            int tempLevel = changeLevel;
        leableA:
            tempLevel = tempLevel % 3;
            try
            {
                int currBlank = Convert.ToInt32(((float)UseBlackWidth[tempLevel] * 7620 / (template.Height * CurentPrinter.PDPI) / 100) * inHeigth);
                if (currBlank > 1500)
                {
                    currBlank = 1500;
                }
                //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + tempLevel);
                //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + MaxWidth + "|" + inHeigth + "|" + barcodeWidth + "|" + currBlank + "|" + UseComboxNum[tempLevel] + "|" + UseCountLevel[tempLevel]);

                sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, barcodeWidth, currBlank, UseComboxNum[tempLevel], UseCountLevel[tempLevel]);//
            }
            catch(Exception ex)
            {
               // WriteLogForCheck.WriteLogFile(DateTime.Now + ":调用Function3出错" + ex.Message);

                throw ex;
            }
            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }
            


            iMageInfo[] arrResult = (iMageInfo[])sizeResult;
            //foreach (var aa in arrResult)
            //{
            //    int bb = 1;
            //    WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "aa=" + " " + aa.Width + " " + aa.Height + " ");
            //    bb++;
            //}

            //此处为处理找不到片子时放宽Level
            if (arrResult[0].Count == 0)
            {
                if (tempLevel >=2)
                {
                   // WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "tempLevel=" + tempLevel);

                    LeftH = 0;
                    return new List<PhotoInfo_PBDCT>();
                }
                else
                {
                    tempLevel++;
                    goto leableA;
                }


            }


            //此处判断小片

            int maxphotoWidth = arrResult.Max(p => p.Width);
            int maxphotoHeight = arrResult.Max(p => p.Height);
            //decimal colphotoNum = (decimal)template.Height / template.MinPhotoSize;
            if ((maxphotoWidth <= template.MinPhotoSize * CurentPrinter.PDPI && maxphotoHeight < template.MinPhotoSize * CurentPrinter.PDPI) && currRang.Height >= template.Height * CurentPrinter.PDPI * 0.5)
            {
                if (tempLevel < 2)
                {
                    tempLevel++;
                    goto leableA;
                }
                else
                {

                    if (currRang.Height >= template.Height * CurentPrinter.PDPI * 0.5 && currRang.X >= MinLength)
                    {
                        int testoutHeightLeft = 0;
                        List<PhotoInfo_PBDCT> Templist = AllList.Where(p => p.IsInBoard == false && ((p.Photo_Base_Info.PixFactHeigth <= template.Height * CurentPrinter.PDPI && p.Photo_Base_Info.PixFactWidth <= template.Length * CurentPrinter.PDPI) || (p.Photo_Base_Info.PixFactHeigth <= template.Length * CurentPrinter.PDPI && p.Photo_Base_Info.PixFactWidth <= template.Height * CurentPrinter.PDPI))).ToList(); ;

                        List<PhotoInfo_PBDCT> PhotoTestList = FindSuitPhotos2(Templist, new Rectangle(new Point(0, 0), new Size((int)template.Length * CurentPrinter.PDPI, (int)template.Height * CurentPrinter.PDPI)), template.BarCodeStatus, out testoutHeightLeft, AllList, 0);

                        double maxphotoWidthTest = PhotoTestList.Max(p => p.Photo_Base_Info.Width);
                        double maxphotoHeightTest = PhotoTestList.Max(p => p.Photo_Base_Info.Height);
                        if (maxphotoWidthTest <= (double)template.MinPhotoSize && maxphotoWidthTest < (double)template.MinPhotoSize)
                        {
                            //不处理放进版面
                        }
                        else
                        {
                            //认为次情况不能取到照片
                            LeftH = 0;
                            return new List<PhotoInfo_PBDCT>();
                        }

                    }
                    else
                    {
                        //不处理放进版面
                    }
                }



            }

            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();

            //WriteLogForCheck.WriteLogFile(DateTime.Now + ":" + "suites.count=" + suitest.Count);


            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority).ThenByDescending(ph => ph.CompletePercent);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                //处理条码
                if (barCodeStatus != SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
                {
                    if (arrResult[i].barCode == 0)
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                    }
                    else
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode);
                }
                //处理旋转
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                else
                {
                    suitTemp.ForEach(en => en.IsWhirl = false);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片



            //确定照片的摆放顺序
            //Point leftTop = currRang.Location;//左上定点
            //Point leftBottom = new Point(currRang.Location.X, currRang.Location.Y + currRang.Height);//坐下顶点



            //suitest.Add(photoSizeGroup.FirstOrDefault());
            //suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            if (putPhotoOrder == 0)
            {
                suitest = suitest.OrderBy(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            else
            {
                suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            int col = 0;
            int CurrHeight = currRang.Location.Y; ;

            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                //if (photo.Location.X == 0 && photo.Location.Y == 0)
                //{

                //}

            }
            int newCurrHeight = suitest.Max(p => p.Location.Y + (p.IsWhirl != true ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth) + (p.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom ? BARCODEWIDTH : 0));
            //LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满
            if (newCurrHeight != CurrHeight)
            {

            }
            LeftH = inHeigth - newCurrHeight + currRang.Location.Y;
            if (LeftH <= 0)
            {

            }
            else
            {

                if (inVPoint.Count > 0)
                {
                    Point FirstPoint = inVPoint.Where(p => p.Y >= currRang.Y).OrderBy(or => or.Y).FirstOrDefault();
                    Point LastPoint = inVPoint.Where(p => p.Y <= currRang.Y + currRang.Height).OrderByDescending(or => or.Y).FirstOrDefault();
                    int topHeight = FirstPoint.Y - currRang.Y;
                    if (FirstPoint.Y < currRang.Location.Y)
                    {

                    }
                    else
                    {
                        int bottomHeight = currRang.Y + currRang.Height - LastPoint.Y;
                        if (LeftH < topHeight && LeftH < bottomHeight)
                        {
                            //移动不起作用
                        }
                        else if (LeftH <= topHeight && LeftH >= bottomHeight)
                        {
                            //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                            foreach (var photo in suitest)
                            {
                                photo.Location = new Point(photo.Location.X, photo.Location.Y + (LeftH - bottomHeight));

                            }

                        }
                        else if (LeftH >= topHeight && LeftH <= bottomHeight)
                        {
                            //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                            foreach (var photo in suitest)
                            {
                                photo.Location = new Point(photo.Location.X, photo.Location.Y + topHeight);

                            }
                        }
                        else if (LeftH > topHeight && LeftH > bottomHeight)
                        {
                            if (topHeight < bottomHeight)
                            {
                                //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                                foreach (var photo in suitest)
                                {
                                    photo.Location = new Point(photo.Location.X, photo.Location.Y + topHeight);

                                }
                            }
                            else
                            {
                                //不动默认往上放 但是照片要往下移动=的距离靠近缝隙
                                foreach (var photo in suitest)
                                {
                                    photo.Location = new Point(photo.Location.X, photo.Location.Y + (LeftH - bottomHeight));

                                }
                            }
                        }
                    }



                }

                int afterBottomHeight = suitest.Max(p => p.Location.Y + (p.IsWhirl != true ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth) + (p.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom ? BARCODEWIDTH : 0));
                if (afterBottomHeight > currRang.Y + currRang.Height)
                {

                }
                int aftertopHeight = suitest.Min(p => p.Location.Y);
                if (afterBottomHeight < currRang.Y)
                {

                }

            }
            return suitest;

        }
        /// <summary>
        /// 获取一组照片
        /// </summary>
        /// <param name="inList">可用照片列表</param>
        /// <param name="currRang">当前填充矩形框</param>
        /// <param name="barCodeStatus">条码放置方向</param>
        /// <param name="LeftH">剩余高度</param>
        /// <param name="AllList"></param>
        /// <param name="putPhotoOrder">照片放置宽度顺序</param>
        /// <param name="template">当前模板</param>
        /// <param name="CurentPrinter">当前打印机</param>
        /// <param name="MinLength">最小版长</param>
        /// <param name="autoModel">拼版模式</param>
        /// <returns></returns>
        List<PhotoInfo_PBDCT> FindSuitPhotos21(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, int putPhotoOrder, BoardTemplate_DCT template, PrinterDCT CurentPrinter, int MinLength, int aotuModel)
        {

            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            //var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth).OrderBy(or => or.Photo_Base_Info.PixFactHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).OrderByDescending(or => or.Photo_Base_Info.PixFactHeigth).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.FirstOrDefault().Photo_Base_Info.PixFactHeigth).ToList();

            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }
            int[] UseBlackWidth = new int[3];
            int[] UseComboxNum = new int[3];
            int[] UseCountLevel = new int[3];
            if (aotuModel == 0)
            {
                UseBlackWidth = BlackWidthLong;
                UseComboxNum = ComboxNumLong;
                UseCountLevel = CountLevelLong;
            }
            else if (aotuModel == 1)
            {
                UseBlackWidth = BlackWidthLowWaste;
                UseComboxNum = BlackWidthLowWaste;
                UseCountLevel = BlackWidthLowWaste;
            }
            else
            {
                UseBlackWidth = BlackWidthLong;
                UseComboxNum = ComboxNumLong;
                UseCountLevel = CountLevelLong;
            }
            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;
            int barcodeWidth = 30;
            if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
            {
                barcodeWidth = 0;
            }
            int tempLevel = changeLevel;
        leableA:
            try
            {
                int currBlank=Convert.ToInt32(((float)UseBlackWidth[tempLevel] * 7620 / (template.Height*CurentPrinter.PDPI)/ 100) * inHeigth);
                if (currBlank > 1500)
                {
                    currBlank = 1500;
                }
                sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, barcodeWidth, currBlank, UseComboxNum[tempLevel], UseCountLevel[tempLevel]);//
            }
            catch
            {

            }
            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }


            iMageInfo[] arrResult = (iMageInfo[])sizeResult;

            //此处为处理找不到片子时放宽Level
            if (arrResult[0].Count == 0)
            {
                if (tempLevel >= 2)
                {
                    LeftH = 0;
                    return new List<PhotoInfo_PBDCT>();
                }
                else
                {
                    tempLevel++;
                    goto leableA;
                }


            }


            //此处判断小片

            int maxphotoWidth = arrResult.Max(p => p.Width);
            int maxphotoHeight = arrResult.Max(p => p.Height);
            //decimal colphotoNum = (decimal)template.Height / template.MinPhotoSize;
            if ((maxphotoWidth <= template.MinPhotoSize * CurentPrinter.PDPI && maxphotoHeight < template.MinPhotoSize * CurentPrinter.PDPI) && currRang.Height >= template.Height * CurentPrinter.PDPI * 0.5)
            {
                if (tempLevel < 2)
                {
                    tempLevel++;
                    goto leableA;
                }
                else
                {

                    if (currRang.Height >= template.Height * CurentPrinter.PDPI * 0.5 && currRang.X >= MinLength)
                    {
                        int testoutHeightLeft = 0;
                        List<PhotoInfo_PBDCT> Templist = AllList.Where(p => p.IsInBoard == false && ((p.Photo_Base_Info.PixFactHeigth <= template.Height * CurentPrinter.PDPI && p.Photo_Base_Info.PixFactWidth <= template.Length * CurentPrinter.PDPI) || (p.Photo_Base_Info.PixFactHeigth <= template.Length * CurentPrinter.PDPI && p.Photo_Base_Info.PixFactWidth <= template.Height * CurentPrinter.PDPI))).ToList(); ;

                        List<PhotoInfo_PBDCT> PhotoTestList = FindSuitPhotos2(Templist, new Rectangle(new Point(0, 0), new Size((int)template.Length * CurentPrinter.PDPI, (int)template.Height * CurentPrinter.PDPI)), template.BarCodeStatus, out testoutHeightLeft, AllList, 0);

                        double maxphotoWidthTest = PhotoTestList.Max(p => p.Photo_Base_Info.Width);
                        double maxphotoHeightTest = PhotoTestList.Max(p => p.Photo_Base_Info.Height);
                        if (maxphotoWidthTest <= (double)template.MinPhotoSize && maxphotoWidthTest < (double)template.MinPhotoSize)
                        {
                            //不处理放进版面
                        }
                        else
                        {
                            //认为次情况不能取到照片
                            LeftH = 0;
                            return new List<PhotoInfo_PBDCT>();
                        }

                    }
                    else
                    {
                        //不处理放进版面
                    }
                }



            }

            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();



            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority).ThenByDescending(ph => ph.CompletePercent);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                //处理条码
                if (barCodeStatus != SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
                {
                    if (arrResult[i].barCode == 0)
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                    }
                    else
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode);
                }
                //处理旋转
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                else
                {
                    suitTemp.ForEach(en => en.IsWhirl = false);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片



            //确定照片的摆放顺序
            //Point leftTop = currRang.Location;//左上定点
            //Point leftBottom = new Point(currRang.Location.X, currRang.Location.Y + currRang.Height);//坐下顶点



            //suitest.Add(photoSizeGroup.FirstOrDefault());
            //suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            if (putPhotoOrder == 0)
            {
                suitest = suitest.OrderBy(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            else
            {
                suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            int col = 0;
            int CurrHeight = currRang.Location.Y;
            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                //if (photo.Location.X == 0 && photo.Location.Y == 0)
                //{

                //}

            }
            LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满

            if (LeftH <= 0)
            {

            }
            else
            {
                //if (putPhotoOrder == 0)
                //{ //suitest.ForEach(p => p.Location = new Point(p.Location.X, p.Location.Y + LeftH));
                //    foreach (var photo in suitest)
                //    {
                //        photo.Location = new Point(photo.Location.X, photo.Location.Y + LeftH);

                //    }
                //}
                //else
                //{

                //}
            }
            return suitest;

        }
        /// <summary>
        /// 强制获取一张出来
        /// </summary>
        /// <param name="inList">可用照片列表</param>
        /// <param name="currRang">当前填充矩形框</param>
        /// <param name="barCodeStatus">条码放置方向</param>
        /// <param name="LeftH">剩余高度</param>
        /// <param name="AllList"></param>
        /// <param name="putPhotoOrder">照片放置宽度顺序</param>
        /// <param name="template">当前模板</param>
        /// <param name="CurentPrinter">当前打印机</param>
        /// <param name="MinLength">最小版长</param>
        /// <returns></returns>
        List<PhotoInfo_PBDCT> FindSuitPhotosForce(List<PhotoInfo_PBDCT> inList, Rectangle currRang, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus, out int LeftH, List<PhotoInfo_PBDCT> AllList, int putPhotoOrder, BoardTemplate_DCT template, PrinterDCT CurentPrinter, int MinLength, List<Point> inVPoint)
        {

            int inHeigth = currRang.Height; int MaxWidth = currRang.Width;
            //var TEMP = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth);
            //var TEMP1 inList= .Where(g => g.Photo_Base_Info.PixFactHeigth <= inHeigth);
            //var TEMP2 = inList.Where(g => g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth).OrderBy(or => or.Photo_Base_Info.PixFactHeigth);
            var photoSizeGroup = inList.Where(g => (g.Photo_Base_Info.PixFactWidth <= MaxWidth && g.Photo_Base_Info.PixFactHeigth <= inHeigth) || (g.Photo_Base_Info.PixFactWidth <= inHeigth && g.Photo_Base_Info.PixFactHeigth <= MaxWidth)).OrderByDescending(or => or.Photo_Base_Info.PixFactHeigth).GroupBy(p => p.PhotoSizePix).ToList();
            if (photoSizeGroup.Count() == 0)
            {
                LeftH = currRang.Height;
                return null;
            }
            photoSizeGroup = photoSizeGroup.OrderByDescending(or => or.FirstOrDefault().Photo_Base_Info.PixFactHeigth).ToList();

            int sizeCount = photoSizeGroup.Count();
            iMageInfo[] sizeArray = new iMageInfo[sizeCount + 1];
            for (int i = 1; i <= sizeCount; i++)
            {
                iMageInfo sizeG = new iMageInfo();
                sizeG.Width = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactWidth;
                sizeG.Height = photoSizeGroup[i - 1].FirstOrDefault().Photo_Base_Info.PixFactHeigth;
                sizeG.Count = photoSizeGroup[i - 1].Count();

                sizeArray[i] = sizeG;
            }
            Array array = sizeArray;
            //第一个参数 数组，第二个第三个 是宽高第四个是条码宽第五个是 容错宽 第六个是组合数 第7个一次取最多尺寸数
            Array sizeResult = null;
            int barcodeWidth = 30;
            if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
            {
                barcodeWidth = 0;
            }
            int tempLevel = 2;

            try
            {
                int currBlack = Convert.ToInt32(((float)BlackWidthForce[tempLevel] * 7620 / (template.Height * CurentPrinter.PDPI) / 100) * inHeigth);

                if (currBlack > 1500)
                {
                    currBlack = 1500;
                }
                sizeResult = InterFace.Function3(ref array, MaxWidth, inHeigth, barcodeWidth, currBlack, ComboxNumForce[tempLevel], CountLevelForce[tempLevel]);//
            }
            catch
            {
                throw new Exception("请确认已装入加密狗后再试");
            }
            if (sizeResult.Length == 0)
            {
                throw new Exception("请确认已装入加密狗后再试");
            }


            iMageInfo[] arrResult = (iMageInfo[])sizeResult;





            string Key = "";
            Dictionary<string, int> sizePair = new Dictionary<string, int>();
            List<PhotoInfo_PBDCT> suitest = new List<PhotoInfo_PBDCT>();



            Dictionary<int, int> orderAndNum = new Dictionary<int, int>();//orderid和缺少多少张就完成
            List<int> AddedPhotoidlist = new List<int>();
            for (int i = 0; i < arrResult.Length; i++)
            {
                List<PhotoInfo_PBDCT> suitTemp = new List<PhotoInfo_PBDCT>();
                var a = inList.Where(p => p.Photo_Base_Info.PixFactWidth == arrResult[i].Width && p.Photo_Base_Info.PixFactHeigth == arrResult[i].Height);
                var b = a.OrderByDescending(or => or.Priority).ThenByDescending(ph => ph.CompletePercent);
                int icount = 0;
                foreach (var ph in b)
                {
                    if (AddedPhotoidlist.Contains(ph.PhotoId))
                    {
                        continue;
                    }
                    else
                    {
                        AddedPhotoidlist.Add(ph.PhotoId);
                        suitTemp.Add(ph);
                        icount++;
                        if (icount == arrResult[i].Count)
                        {
                            break;
                        }
                    }
                }
                //处理条码
                if (barCodeStatus != SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode)
                {
                    if (arrResult[i].barCode == 0)
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft);

                    }
                    else
                    {
                        suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom);
                    }
                }
                else
                {
                    suitTemp.ForEach(eh => eh.BarCodeStatus = SmallERP.COM.Status.PhotoBarCodeStatus.NoBarCode);
                }
                //处理旋转
                if (arrResult[i].isRotate == 1)
                {
                    suitTemp.ForEach(en => en.IsWhirl = true);
                }
                else
                {
                    suitTemp.ForEach(en => en.IsWhirl = false);
                }
                suitest.AddRange(suitTemp);



            }

            //新的获取照片顺序的按照剩余照片最少的 产品的原则获取照片



            //确定照片的摆放顺序
            //Point leftTop = currRang.Location;//左上定点
            //Point leftBottom = new Point(currRang.Location.X, currRang.Location.Y + currRang.Height);//坐下顶点



            //suitest.Add(photoSizeGroup.FirstOrDefault());
            //suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            if (putPhotoOrder == 0)
            {
                suitest = suitest.OrderBy(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            else
            {
                suitest = suitest.OrderByDescending(p => (p.IsWhirl) ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth).ToList();
            }
            int col = 0;
            int CurrHeight = currRang.Location.Y;
            foreach (var photo in suitest)
            {

                //计算合适的 照片的坐标
                if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth + BARCODEWIDTH;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH;
                    }

                }
                else if (photo.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                else
                {
                    if (photo.IsWhirl)
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactWidth;
                    }
                    else
                    {
                        photo.Location = new Point(currRang.Location.X, CurrHeight);
                        CurrHeight += photo.Photo_Base_Info.PixFactHeigth;
                    }
                }
                //if (photo.Location.X == 0 && photo.Location.Y == 0)
                //{

                //}

            }
            int newCurrHeight = suitest.Max(p => p.Location.Y + (p.IsWhirl != true ? p.Photo_Base_Info.PixFactHeigth : p.Photo_Base_Info.PixFactWidth) + (p.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom ? BARCODEWIDTH : 0));

            LeftH = inHeigth - CurrHeight + currRang.Location.Y;//还差多少可以填满

            if (LeftH <= 0)
            {

            }
            else
            {
                if (inVPoint.Count > 0)
                {
                    Point FirstPoint = inVPoint.Where(p => p.Y >= currRang.Y).OrderBy(or => or.Y).FirstOrDefault();
                    Point LastPoint = inVPoint.Where(p => p.Y <= currRang.Y + currRang.Height).OrderByDescending(or => or.Y).FirstOrDefault();
                    int topHeight = FirstPoint.Y - currRang.Y;
                    int bottomHeight = currRang.Y + currRang.Height - LastPoint.Y;
                    if (FirstPoint.Y < currRang.Location.Y)
                    {

                    }
                    else
                    {
                        if (LeftH < topHeight && LeftH < bottomHeight)
                        {
                            //移动不起作用
                        }
                        else if (LeftH <= topHeight && LeftH >= bottomHeight)
                        {
                            //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                            foreach (var photo in suitest)
                            {
                                photo.Location = new Point(photo.Location.X, photo.Location.Y + (LeftH - bottomHeight));

                            }

                        }
                        else if (LeftH >= topHeight && LeftH <= bottomHeight)
                        {
                            //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                            foreach (var photo in suitest)
                            {
                                photo.Location = new Point(photo.Location.X, photo.Location.Y + topHeight);

                            }
                        }
                        else if (LeftH > topHeight && LeftH > bottomHeight)
                        {
                            if (topHeight < bottomHeight)
                            {
                                //不动默认往上放 但是照片要往下移动的距离靠近缝隙
                                foreach (var photo in suitest)
                                {
                                    photo.Location = new Point(photo.Location.X, photo.Location.Y + topHeight);

                                }
                            }
                            else
                            {
                                //不动默认往上放 但是照片要往下移动=的距离靠近缝隙
                                foreach (var photo in suitest)
                                {
                                    photo.Location = new Point(photo.Location.X, photo.Location.Y + (LeftH - bottomHeight));

                                }
                            }
                        }
                    }



                }
            }
            return suitest;

        }
        /// <summary>
        /// 剩余空间全自动拼板
        /// </summary>
        /// <param name="sourPList"></param>
        /// <param name="r_StartPoint"></param>
        /// <param name="r_Size"></param>
        /// <param name="resultPList"></param>
        /// <param name="barCodeStatus"></param>
        private void Re_AutoPB(ref List<PhotoInfo_PBDCT> sourPList, Point r_StartPoint, Size r_Size, ref List<PhotoInfo_PBDCT> resultPList, SmallERP.COM.Status.PhotoBarCodeStatus barCodeStatus)
        {
            Point XDstart_Point = new Point(0, 0);      //待拼板区域的相对坐标
            List<PhotoInfo_PBDCT> TSPlist;             //TSPlist:Temp Source Picture List

            Point re_Point1 = new Point(0, 0);          //剩余空间1的起始坐标
            Size re_Size1 = new Size(0, 0);             //剩余空间1的SIZE

            Point re_Point2 = new Point(0, 0);          //剩余空间2的起始坐标
            Size re_Size2 = new Size(0, 0);             //剩余空间2的SIZE

            Point re_Point3 = new Point(0, 0);          //剩余空间3的起始坐标
            Size re_Size3 = new Size(0, 0);             //剩余空间3的SIZE


            TSPlist = sourPList.Where(p => !p.IsInBoard && ((p.Photo_Base_Info.PixHeight <= r_Size.Height && p.Photo_Base_Info.PixWidth <= r_Size.Width) || (p.Photo_Base_Info.PixHeight <= r_Size.Width && p.Photo_Base_Info.PixWidth <= r_Size.Height))).OrderByDescending(p => p.Photo_Base_Info.PixHeight).ToList();//按高度从大到小排序

            if (TSPlist == null || TSPlist.Count < 1)
            {
                return;
            }

            foreach (var item in TSPlist.GroupBy(p => p.PSize, new MySizeComparer()).ToList())
            {
                int colum_row = 0;      //每一列可以放入多少个同SIZE的图片
                int colums_fact = 0;    //此类尺寸的图片实际有多少列
                int picNum = 0;         //实际排了多少张此类尺寸的图片
                Size s = item.Key;      //就是能放入剩余空间的图片SIZE的模板，与此模板相同的图片iswhirl=false 相反的就是true;

                #region 找到适合这一区哉能拼的那类图片的集合

                if (XDstart_Point.X + s.Width > r_Size.Width || XDstart_Point.Y + s.Height > r_Size.Height)
                {
                    if (XDstart_Point.X + s.Height > r_Size.Width || XDstart_Point.Y + s.Width > r_Size.Height)
                    {
                        continue;
                    }
                    else
                    {
                        s.Width = item.Key.Height;
                        s.Height = item.Key.Width;
                    }
                }


                if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                {
                    s.Width += BARCODEWIDTH;
                }
                else if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
                {
                    s.Height += BARCODEWIDTH;
                }

                colum_row = r_Size.Height / s.Height;
                int row_colum = r_Size.Width / s.Width;
                if (colum_row < 1 || row_colum < 1)
                {
                    continue;
                }
                #endregion

                //找到这类集合后对这些图片进行全拼板
                foreach (PhotoInfo_PBDCT p in item.OrderByDescending(pt => pt.Priority).ToList())//按加急级别排倒序的同一SIZE的图片
                {
                    #region 在某一区哉里对相同尺寸的照片进行拼板

                    if (p.IsInBoard)
                    {
                        continue;
                    }
                    if (XDstart_Point.Y + s.Height > r_Size.Height)
                    {
                        if (XDstart_Point.X + 2 * s.Width > r_Size.Width)
                        {
                            break;//此区域内不能再放下相同尺寸的照片了
                        }
                        else//换列
                        {
                            XDstart_Point.Y = 0;
                            XDstart_Point.X += s.Width;
                        }
                    }

                    if (barCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
                    {
                        if (s.Width - BARCODEWIDTH == p.Photo_Base_Info.PixHeight)
                        {
                            p.IsWhirl = true;
                        }

                    }
                    else//条码在下方或是无条码
                    {
                        if (s.Width == p.Photo_Base_Info.PixHeight)
                        {
                            p.IsWhirl = true;
                        }
                    }
                    p.Location = new Point(r_StartPoint.X + XDstart_Point.X, r_StartPoint.Y + XDstart_Point.Y);
                    p.BarCodeStatus = barCodeStatus;
                    p.IsInBoard = true;
                    p.IsAutoPB = true;
                    if (resultPList.Find(pt => pt.PhotoId == p.PhotoId) == null)
                    {
                        resultPList.Add(p);

                        XDstart_Point.Y += s.Height;
                        picNum++;
                    }
                    else
                    {
                        Debug.WriteLine("同一张图片被自动拼板拼了二次!" + p.PhotoId);
                    }

                    #endregion
                }

                if (picNum == 0)//此类尺寸的图片一个都不行，就换下一类尺寸的图片
                {
                    continue;
                }

                #region 得到各自相应的剩余可拼板的区哉
                if (picNum <= colum_row)//只有一列
                {
                    re_Point1 = new Point(XDstart_Point.X, picNum * s.Height);
                    re_Size1 = new Size(s.Width, r_Size.Height - re_Point1.Y);

                    re_Point2 = new Point(s.Width, 0);
                    re_Size2 = new Size(r_Size.Width - s.Width, r_Size.Height);
                }
                else
                {
                    colums_fact = picNum / colum_row;//排满的列数

                    if (picNum % colum_row == 0)//刚好排满几列
                    {
                        re_Point1 = new Point(0, colum_row * s.Height);
                        re_Size1 = new Size(s.Width * colums_fact, r_Size.Height - re_Point1.Y);

                        re_Point2 = new Point(s.Width * colums_fact, 0);
                        re_Size2 = new Size(r_Size.Width - re_Point2.X, r_Size.Height);
                    }
                    else
                    {
                        re_Point1 = new Point(0, colum_row * s.Height);
                        re_Size1 = new Size(s.Width * colums_fact, r_Size.Height - re_Point1.Y);

                        re_Point2 = new Point(s.Width * colums_fact, s.Height * (picNum % colum_row));
                        re_Size2 = new Size(s.Width, r_Size.Height - re_Point2.Y);

                        re_Point3 = new Point(s.Width * colums_fact + s.Width, 0);
                        re_Size3 = new Size(r_Size.Width - re_Point3.X, r_Size.Height);
                    }
                }
                #endregion

                #region 递归对各个区哉进行全自动拼板算法
                if (re_Size1.Width > 0 && re_Size1.Height > 0)
                {
                    re_Point1.Offset(r_StartPoint);
                    Re_AutoPB(ref sourPList, re_Point1, re_Size1, ref resultPList, barCodeStatus);
                }

                if (re_Size2.Width > 0 && re_Size2.Height > 0)
                {
                    re_Point2.Offset(r_StartPoint);
                    Re_AutoPB(ref sourPList, re_Point2, re_Size2, ref resultPList, barCodeStatus);
                }

                if (re_Size3.Width > 0 && re_Size3.Height > 0)
                {
                    re_Point3.Offset(r_StartPoint);
                    Re_AutoPB(ref sourPList, re_Point3, re_Size3, ref resultPList, barCodeStatus);
                }

                #endregion

                break;
            }
        }


        #region 辅助比较类

        class MyOrderInfoCompare : IEqualityComparer<Order_InfoDCT>//辅助类，用于比较orderInfo
        {
            #region IEqualityComparer<orderInfo> 成员

            public bool Equals(Order_InfoDCT x, Order_InfoDCT y)
            {
                if (x.OrderProductId == y.OrderProductId)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public int GetHashCode(Order_InfoDCT obj)
            {
                return obj.GetHashCode();
            }

            #endregion
        }

        class MyPhotoComparer : IEqualityComparer<PhotoInfo_PBDCT>
        {
            #region IEqualityComparer<PhotoInfo_PBDCT> 成员

            public bool Equals(PhotoInfo_PBDCT x, PhotoInfo_PBDCT y)
            {
                if (x.PhotoId == y.PhotoId)
                {
                    return true;
                }
                return false;
            }

            public int GetHashCode(PhotoInfo_PBDCT obj)
            {
                //return obj.GetHashCode();
                return -1;
            }

            #endregion
        }
        //class MyDiectComparer : IEqualityComparer<PhotoInfo_PBDCT>
        //{
        //    #region IEqualityComparer<PhotoInfo_PBDCT> 成员

        //    public bool Equals(PhotoInfo_PBDCT x, PhotoInfo_PBDCT y)
        //    {
        //        if (x.PhotoId == y.PhotoId)
        //        {
        //            return true;
        //        }
        //        return false;
        //    }

        //    public int GetHashCode(PhotoInfo_PBDCT obj)
        //    {
        //        //return obj.GetHashCode();
        //        return -1;
        //    }

        //    #endregion
        //}
        public class MysizeComparerBig : IComparer<Size>
        {
            public int Compare(Size x, Size y)
            {
                int Xbig = x.Height > x.Width ? x.Height : x.Width;
                int Ybig = y.Height > y.Width ? y.Height : y.Width;
                if (Xbig > Ybig)
                {
                    return 1;
                }
                else if (Xbig == Ybig)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }


        }
        public class MyPointComparer : IComparer<Point>
        {
            public int Compare(Point x, Point y)
            {
                if (x.X < y.X)
                {
                    return -1;
                }
                else if (x.X == y.X)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }


        }
        public class MySizeComparer : IEqualityComparer<Size>
        {
            #region IEqualityComparer<Size> 成员

            public bool Equals(Size x, Size y)
            {
                if ((x.Width == y.Width && x.Height == y.Height) || (x.Width == y.Height && x.Height == y.Width))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public int GetHashCode(Size obj)
            {
                //return obj.GetHashCode();
                return -1;//如果是值的话就返回-1
            }

            #endregion
        }
        public class WasteAndListPhoto
        {
            public WasteAndListPhoto(double w, List<PhotoInfo_PBDCT> inList)
            {
                waste = w;
                PhotoList = inList;
            }
            public double waste { get; set; }
            public List<PhotoInfo_PBDCT> PhotoList { get; set; }
        }
        public class MyStringComparerHeight : IComparer<string>
        {
            #region IEqualityComparer<Size> 成员

            public int Compare(string x, string y)
            {
                if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
                {
                    return 0;
                }
                else
                {
                    x = x.ToUpper();
                    y = y.ToUpper();
                    if (x.IndexOf("X") < 0 || y.IndexOf("X") < 0)
                    {
                        return 0;

                    }
                    else
                    {
                        //int xV = Convert.ToInt32(x.Substring(0, x.IndexOf("X")));
                        //int yV = Convert.ToInt32(y.Substring(0, y.IndexOf("X")));
                        //if (xV > yV)
                        //{
                        //    return 1;
                        //}
                        //else if (xV == yV)
                        //{
                        //    return 0;
                        //}
                        //else
                        //{
                        //    return -1;
                        //}
                        int xV = Convert.ToInt32(x.Substring(x.IndexOf("X") + 1));
                        int yV = Convert.ToInt32(y.Substring(y.IndexOf("X") + 1));
                        if (xV > yV)
                        {
                            return 1;
                        }
                        else if (xV == yV)
                        {
                            return 0;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }

            }

            public int GetHashCode(Size obj)
            {
                //return obj.GetHashCode();
                return -1;//如果是值的话就返回-1
            }

            #endregion

            #region IComparer<string> 成员

            int IComparer<string>.Compare(string x, string y)
            {
                if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
                {
                    return 0;
                }
                else
                {
                    x = x.ToUpper();
                    y = y.ToUpper();
                    if (x.IndexOf("X") < 0 || y.IndexOf("X") < 0)
                    {
                        return 0;

                    }
                    else
                    {
                        double xV = Convert.ToDouble(x.Substring(0, x.IndexOf("X")));
                        double yV = Convert.ToDouble(y.Substring(0, y.IndexOf("X")));
                        if (xV > yV)
                        {
                            return 1;
                        }
                        else if (xV == yV)
                        {
                            return 0;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
            }

            #endregion
        }
        public class MyStringComparer : IComparer<string>
        {
            #region IEqualityComparer<Size> 成员

            public int Compare(string x, string y)
            {
                if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
                {
                    return 0;
                }
                else
                {
                    x = x.ToUpper();
                    y = y.ToUpper();
                    if (x.IndexOf("X") < 0 || y.IndexOf("X") < 0)
                    {
                        return 0;

                    }
                    else
                    {
                        int xV = Convert.ToInt32(x.Substring(0, x.IndexOf("X")));
                        int yV = Convert.ToInt32(y.Substring(0, y.IndexOf("X")));
                        if (xV > yV)
                        {
                            return 1;
                        }
                        else if (xV == yV)
                        {
                            return 0;
                        }
                        else
                        {
                            return -1;
                        }
                        //int xV = Convert.ToInt32(x.Substring(x.IndexOf("X")+1));
                        //int yV = Convert.ToInt32(y.Substring(y.IndexOf("X")+1));
                        //if (xV > yV)
                        //{
                        //    return 1;
                        //}
                        //else if (xV == yV)
                        //{
                        //    return 0;
                        //}
                        //else
                        //{
                        //    return -1;
                        //}
                    }
                }

            }

            public int GetHashCode(Size obj)
            {
                //return obj.GetHashCode();
                return -1;//如果是值的话就返回-1
            }

            #endregion

            #region IComparer<string> 成员

            int IComparer<string>.Compare(string x, string y)
            {
                if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
                {
                    return 0;
                }
                else
                {
                    x = x.ToUpper();
                    y = y.ToUpper();
                    if (x.IndexOf("X") < 0 || y.IndexOf("X") < 0)
                    {
                        return 0;

                    }
                    else
                    {
                        double xV = Convert.ToDouble(x.Substring(0, x.IndexOf("X")));
                        double yV = Convert.ToDouble(y.Substring(0, y.IndexOf("X")));
                        if (xV > yV)
                        {
                            return 1;
                        }
                        else if (xV == yV)
                        {
                            return 0;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
            }

            #endregion
        }
        #endregion

        /// <summary>
        /// 打印大板
        /// </summary>
        /// <param name="PicList"></param>
        /// <param name="boardTemplate_Info"></param>
        /// <param name="printer_Info"></param>
        /// <param name="wasteRate"></param>
        /// <param name="realBoardLength"></param>
        /// <returns></returns>
        public int CreateBoard(List<PhotoInfo_PBDCT> PicList, BoardTemplate_DCT boardTemplate_Info, PrinterDCT printer_Info, float wasteRate, float realBoardLength)
        {
            //boardLength单位：英寸
            //PicList中的location为屏幕上的坐标
            int boardLength = Convert.ToInt32(realBoardLength * printer_Info.PDPI);

            //photo的location的坐标转换
            PicList.ForEach(p =>
            {
                int x, y;
                x = Convert.ToInt32((float)p.Location.X / PERINCHTOSCREEN * printer_Info.PDPI);
                y = (int)((float)p.Location.Y / PERINCHTOSCREEN * printer_Info.PDPI);
                p.Location = new Point(x, y);
            }
            );
            if (PicList.Max(p => p.Location.X >= boardLength) || PicList.Max(p => p.Location.Y >= boardTemplate_Info.Height * printer_Info.PDPI))
            {
                return -1;
            }
            return CreateBoard(PicList, boardTemplate_Info, printer_Info, wasteRate, boardLength, false);
        }
        /// <summary>
        /// 打印大板
        /// </summary>
        /// <param name="PicList"></param>
        /// <param name="boardTemplate_Info"></param>
        /// <param name="printer_Info"></param>
        /// <param name="wasteRate"></param>
        /// <param name="realBoardLength"></param>
        /// <returns></returns>
        public int CreateBoardHand(List<PhotoInfo_PBDCT> PicList, BoardTemplate_DCT boardTemplate_Info, PrinterDCT printer_Info, float wasteRate, float realBoardLength,int inBoardType=0)
        {
            //boardLength单位：英寸
            //PicList中的location为屏幕上的坐标
            int boardLength = Convert.ToInt32(realBoardLength);

            //photo的location的坐标转换
            //PicList.ForEach(p =>
            //{
            //    int x, y;
            //    x = Convert.ToInt32((float)p.Location.X / PERINCHTOSCREEN * printer_Info.PDPI);
            //    y = (int)((float)p.Location.Y / PERINCHTOSCREEN * printer_Info.PDPI);
            //    p.Location = new Point(x, y);
            //}
            //);
            if (PicList.Max(p => p.Location.X >= boardLength) || PicList.Max(p => p.Location.Y >= boardTemplate_Info.Height * printer_Info.PDPI))
            {
                return -1;
            }
            return CreateBoard(PicList, boardTemplate_Info, printer_Info, wasteRate, boardLength, false, inBoardType);
        }
        /// <summary>
        /// 所有的关于长度、宽度、坐标信息全是“图片”上的象素为单位
        /// </summary>
        /// <param name="PicList"></param>
        /// <param name="tb_paperTemplate_Info"></param>
        /// <param name="printer_Info"></param>
        /// <param name="userId"></param>
        /// <param name="wasteRate"></param>
        /// <param name="realBoardLength"></param>
        /// <returns>-2加密失败</returns>
        public int CreateBoard(List<PhotoInfo_PBDCT> PicList, BoardTemplate_DCT tb_paperTemplate_Info, PrinterDCT printer_Info, float wasteRate, int realBoardLength, bool isNeedAddNewTemplate,int boardType=0)
        {

            int result = _OperateDb.CreateBoard(PicList, tb_paperTemplate_Info, printer_Info, wasteRate, realBoardLength, true, BoardFolderPath, boardType);


            return result;
        }
        /// <summary>
        /// 所有的关于长度、宽度、坐标信息全是“图片”上的象素为单位
        /// </summary>
        /// <param name="PicList"></param>
        /// <param name="tb_paperTemplate_Info"></param>
        /// <param name="printer_Info"></param>
        /// <param name="userId"></param>
        /// <param name="wasteRate"></param>
        /// <param name="realBoardLength"></param>
        /// <returns>-2加密失败</returns>
        public int CreateBoard2(List<PhotoInfo_PBDCT> PicList, BoardTemplate_DCT tb_paperTemplate_Info, PrinterDCT printer_Info, float wasteRate, int realBoardLength, bool isNeedAddNewTemplate, bool isSure)
        {
            //PicList.ForEach(p => p.Photo_Base_Info.PixHeight = p.Photo_Base_Info.PixFactHeigth);
            //PicList.ForEach(p => p.Photo_Base_Info.PixWidth = p.Photo_Base_Info.PixFactWidth);
            int result = _OperateDb.CreateBoard2(PicList, tb_paperTemplate_Info, printer_Info, wasteRate, realBoardLength, true, BoardFolderPath, isSure);


            return result;
        }
        public int CombinBoard(List<PhotoInfo_PBDCT> PicList, int transX, int inNewLength, int InOldBoardid, PrinterDCT printer_Info, BoardTemplate_DCT tb_paperTemplate_Info)
        {
            int result = _OperateDb.CombinBoard(PicList, transX, inNewLength, InOldBoardid, printer_Info, tb_paperTemplate_Info);


            return result;
        }
        string istDota = "','";
        /// <summary>
        /// 加密坐标X、Y
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public string EncryptPoint(string strInput)
        {
            if (string.IsNullOrEmpty(_dogCode))
            {
                try
                {
                    _dogCode = GetDogCode();
                }
                catch (Exception)
                {
                    return string.Empty;
                }

                if (string.IsNullOrEmpty(_dogCode))
                    return string.Empty;
            }
            string key = _dogCode.Substring(0, 8);//前八位为KEY
            string encryptDogCode = _dogCode.Substring(8);
            string dogKey = Decipher(encryptDogCode, key);

            return Cipher(dogKey, strInput);
        }


        //加密
        public string Cipher(string key, string input)
        {
            char[] random = key.ToCharArray();
            char[] from_text = input.ToCharArray();
            StringBuilder result = new StringBuilder();
            char[] buf1 = new char[input.Length];
            int offset;
            int str_len = 1;
            int i;
            int ch;
            str_len = from_text.Length;
            try
            {
                for (i = 0; i < str_len; i++)
                {
                    ch = (int)from_text[i];
                    if (ch >= MIN_ASC && ch <= MAX_ASC)
                    {
                        ch = ch - MIN_ASC;
                        offset = (NUM_ASC + 1) * random[i];
                        ch = (ch + offset) % NUM_ASC;
                        ch = ch + MIN_ASC;
                        buf1[i] = (char)ch;
                    }
                    else
                    {
                        buf1[i] = from_text[i];
                    }
                }

                foreach (var item in buf1)
                {
                    result.Append(item);
                }
            }
            catch (Exception)
            {
                result = new StringBuilder("");
            }

            return result.ToString();
        }

        const int MIN_ASC = 32;
        const int MAX_ASC = 126;
        const int NUM_ASC = 95;
        //解密
        public string Decipher(string strFromText, string key)
        {
            StringBuilder result = new StringBuilder();
            char[] random = key.ToCharArray();
            char[] from_text = strFromText.ToCharArray();
            char[] buf1 = new char[strFromText.Length];
            int offset;
            int str_len = 1;
            int i;
            int ch;

            str_len = from_text.Length;
            try
            {
                for (i = 0; i < str_len; i++)
                {
                    ch = (int)from_text[i];
                    if (ch >= MIN_ASC && ch <= MAX_ASC)
                    {
                        ch = ch - MIN_ASC;
                        offset = (NUM_ASC + 1) * random[i];
                        ch = (ch - offset) % NUM_ASC;
                        if (ch < 0)
                        {
                            ch = ch + NUM_ASC;
                        }
                        ch = ch + MIN_ASC;
                        buf1[i] = (char)ch;
                    }
                    else
                    {
                        buf1[i] = from_text[i];
                    }
                }
                foreach (var item in buf1)
                {
                    result.Append(item);
                }
            }
            catch (Exception)
            {
                result = new StringBuilder("");
            }

            return result.ToString();
        }
        /// <summary>
        /// 当生成大图完成时就检查相应的产品是否都完成拼板
        /// </summary>
        /// <param name="list"></param>
        /// <param name="dataBase"></param>
        private void CheckOrderPBIsOver(List<int> list, int dataBase)
        {
            //list = list.Distinct().ToList();
            //int photoWatiPB = (int)SmallERP.COM.Status.PhotoStatus.WaitPB;
            //foreach (var orderProductId in list)
            //{
            //    orderInfo oiT = dataBase.orderInfo.FirstOrDefault(o => o.OrderProductId == orderProductId);
            //    if (!oiT.photoInfo.IsLoaded)
            //    {
            //        oiT.photoInfo.Load();
            //    }
            //    if (oiT.photoInfo.ToList().Count(p => p.Status == photoWatiPB) < 1)
            //    {
            //        //oiT.Status = (int)SmallERP.COM.Status.OrderStatus.;
            //    }

            //    int photoCount = dataBase.photoInfo.Count(p => p.orderInfo.OrderProductId == orderProductId && p.Status == photoWatiPB);
            //    //if (photoCount < 1)
            //    //{
            //    //    //没有等待拼板的了可以将产品的状态改为等待打印
            //    //    dataBase.orderInfo.FirstOrDefault(o => o.OrderProductId == orderProductId).Status = (int)SmallERP.COM.Status.OrderStatus.WaitPrint;
            //    //}
            //}
        }

        /// <summary>
        /// 复制产品
        /// </summary>
        /// <param name="SelectOrderList"></param>
        /// <param name="AddNum"></param>
        /// <returns>-2:向数据库保存时出错</returns>
        public int CopyOrder(List<OrderInfo_PBDCT> SelectOrderList, int AddNum)
        {
            int result = 0;
            result = _OperateDb.CopyOrder(SelectOrderList, AddNum);
            return result;
        }

        /// <summary>
        /// 复制照片
        /// </summary>
        /// <param name="SelectPhotoList"></param>
        /// <param name="AddNum"></param>
        /// <returns></returns>
        public int CopyPhoto(List<PhotoInfo_PBDCT> SelectPhotoList, int AddNum)
        {
            int result = 0;

            result = _OperateDb.CopyPhoto(SelectPhotoList, AddNum);
            return result;
        }

        /// <summary>
        /// 删除选中的照片
        /// </summary>
        /// <param name="SelectPhotoList"></param>
        /// <returns></returns>
        public int DeletePhoto(List<PhotoInfo_PBDCT> SelectPhotoList)
        {
            int result = 0;
            result = _OperateDb.DeletePhotos(SelectPhotoList);
            return result;
        }

        /// <summary>
        /// 删除选中的产品
        /// </summary>
        /// <param name="SelectOrderList"></param>
        /// <returns></returns>
        public int DeleteOrder(List<OrderInfo_PBDCT> SelectOrderList)
        {
            int result = 0;

            result = _OperateDb.DeleteOrders(SelectOrderList);
            return result;
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="modifyOI"></param>
        /// <returns></returns>
        public int ModifyOrder(OrderInfo_PBDCT modifyOI)
        {
            int result = 0;
            result = _OperateDb.ModifyOrder(modifyOI);
            return result;
        }

        public List<PaperFaceDCT> GetAllPaperFace()
        {
            return _OperateDb.GetAllPaperFace();
        }

        public List<PaperTypeDCT> GetAllPaperType()
        {

            return _OperateDb.GetAllPaperType();
        }

        public List<PrinterDCT> GetAllPrinters()
        {
            var aaa = _OperateDb.GetAllPrinters();
            return aaa;
        }





        public int ChangeMakeSure()
        {
            return _OperateDb.ChangeWaitSureToWaitMake();
        }

        public int DeleteBoards(List<int> BoardIDList)
        {
            return _OperateDb.DeleteBoards(BoardIDList);
        }

        public List<DisPalyBoardInfoDCT> GetUnSureBoardInfo()
        {
            return _OperateDb.GetUnSureBoardInfo();
        }

        public bool UpdateOrderDex(OrderInfo_PBDCT oiInfo)
        {
            return _OperateDb.AddDXFContent(oiInfo.order_Base_Info.OrderProductId,oiInfo.order_Base_Info.DXFFilePath, oiInfo.order_Base_Info.DXFContent);
        }

        public bool UpdatePhotoDxf(PhotoInfo_PBDCT photoInfoPbdct)
        {
            return _OperateDb.AddPhotoDXFContent(photoInfoPbdct.Photo_Base_Info.PhotoId, photoInfoPbdct.Photo_Base_Info.DXFFilePath, photoInfoPbdct.Photo_Base_Info.DXFContent);
        }


        /// <summary>
        /// 根据photoid 获取数据
        /// </summary>
        /// <param name="photoId"></param>
        /// <returns></returns>
        public PhotoInfo_PBDCT GetPhotoInfo(int photoId)
        {
            return _OperateDb.GetPhotoInfo(photoId);
        }
    }
    public class MyRectangle
    {
        int BARCODEWIDTH = 30;
        public MyRectangle(PhotoInfo_PBDCT inPhoto)
        {
            myPhoto = inPhoto;
            if (inPhoto.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.RightOrLeft)
            {
                if (inPhoto.IsWhirl)
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH), inPhoto.Photo_Base_Info.PixFactWidth);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
                else
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactWidth + BARCODEWIDTH), inPhoto.Photo_Base_Info.PixFactHeigth);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
            }
            else if (inPhoto.BarCodeStatus == SmallERP.COM.Status.PhotoBarCodeStatus.TopOrBottom)
            {
                if (Photo.IsWhirl)
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactHeigth), inPhoto.Photo_Base_Info.PixFactWidth + BARCODEWIDTH);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
                else
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactWidth), inPhoto.Photo_Base_Info.PixFactHeigth + BARCODEWIDTH);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
            }
            else
            {
                if (Photo.IsWhirl)
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactHeigth), inPhoto.Photo_Base_Info.PixFactWidth);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
                else
                {
                    Size size = new Size((inPhoto.Photo_Base_Info.PixFactWidth), inPhoto.Photo_Base_Info.PixFactHeigth);
                    this.myRact = new Rectangle(inPhoto.Location, size);
                }
            }
            //Size size = new Size(inPhoto.Photo_Base_Info.PixFactWidth, inPhoto.Photo_Base_Info.PixFactHeigth);
            //this.myRact = new Rectangle(inPhoto.Location, size);

        }
        public MyRectangle(Rectangle rant)
        {
            this.myRact = rant;
        }
        private Rectangle myRact { get; set; }
        private PhotoInfo_PBDCT myPhoto { get; set; }
        public PhotoInfo_PBDCT Photo { get { return myPhoto; } }
        public Rectangle PRactangle { get { return this.myRact; } }//当前块
        public bool isUsefull { get; set; }//是否包含照片
        public double Areas { get { return myRact.Width * myRact.Height; } }
        //public MyRectangle LeftRect { get; set; }
        //public MyRectangle RightRect { get; set; }
        //public MyRectangle TopRect { get; set; }
        //public MyRectangle Bottom { get; set; }
        public Point LeftTopPoint { get { return myRact.Location; } }
        public Point RighTopPoint { get { return new Point(myRact.Location.X + myRact.Width, myRact.Location.Y); } }
        public Point LeftBottomPoint { get { return new Point(myRact.Location.X, myRact.Location.Y + myRact.Height); } }
        public Point RightBottomPoint { get { return new Point(myRact.Location.X + myRact.Width, myRact.Location.Y + myRact.Height); } }
    }
    /// <summary>
    /// 拼版中用到的竖线
    /// </summary>
    public class Line
    {

        public Line(Point s, Point e)
        {
            startPoint = s;
            endPoint = e;
        }
        private Point startPoint { get; set; }
        private Point endPoint { get; set; }
        public Point StartPoint { get { return this.startPoint; } set { startPoint = value; } }
        public Point EndPoint { get { return this.endPoint; } set { endPoint = value; } }
        public List<Line> SubtractLine(Line inLine)
        {
            List<Line> rtnList = new List<Line>();
            if (this.startPoint.Y == inLine.startPoint.Y && this.endPoint.Y == inLine.endPoint.Y)//同一条线
            {

            }
            else if (this.startPoint.Y == inLine.startPoint.Y && this.endPoint.Y != inLine.endPoint.Y)//相同的开头
            {
                Line newline = new Line(inLine.endPoint, this.endPoint);
                rtnList.Add(newline);
            }
            else if (this.startPoint.Y != inLine.startPoint.Y && this.endPoint.Y == inLine.endPoint.Y)
            {
                Line newline = new Line(this.startPoint, inLine.startPoint);
                rtnList.Add(newline);
            }
            else
            {
                rtnList.Add(new Line(this.startPoint, inLine.startPoint));
                rtnList.Add(new Line(inLine.endPoint, this.endPoint));
            }
            return rtnList;
        }


       

    }

    public class TempResult
    {
        public List<PhotoInfo_PBDCT> PhotoList { get; set; }
        public decimal Waste { get; set; }
        public int Length { get; set; }
        public BoardTemplate_DCT BoardTemplete{get;set;}
    }
}
